Skip to content

Commit c6866c1

Browse files
TypeScript BotRyanCavanaugh
TypeScript Bot
andauthored
Cherry-pick PR #37021 into release-3.8 (#37025)
Component commits: 864c2d4 Check for undefined `source.symbol` Fixes #37014 Co-authored-by: Ryan Cavanaugh <RyanCavanaugh@users.noreply.github.com>
1 parent d76a45a commit c6866c1

6 files changed

+68
-0
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16153,6 +16153,7 @@ namespace ts {
1615316153
if (unmatchedProperty.valueDeclaration
1615416154
&& isNamedDeclaration(unmatchedProperty.valueDeclaration)
1615516155
&& isPrivateIdentifier(unmatchedProperty.valueDeclaration.name)
16156+
&& source.symbol
1615616157
&& source.symbol.flags & SymbolFlags.Class) {
1615716158
const privateIdentifierDescription = unmatchedProperty.valueDeclaration.name.escapedText;
1615816159
const symbolTableKey = getSymbolNameForPrivateIdentifier(source.symbol, privateIdentifierDescription);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts(2,3): error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher.
2+
tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts(5,7): error TS2741: Property '#field' is missing in type '{}' but required in type 'Class'.
3+
4+
5+
==== tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts (2 errors) ====
6+
export class Class {
7+
#field: any
8+
~~~~~~
9+
!!! error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher.
10+
}
11+
12+
const task: Class = {} as unknown;
13+
~~~~
14+
!!! error TS2741: Property '#field' is missing in type '{}' but required in type 'Class'.
15+
!!! related TS2728 tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts:2:3: '#field' is declared here.
16+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [privateFieldAssignabilityFromUnknown.ts]
2+
export class Class {
3+
#field: any
4+
}
5+
6+
const task: Class = {} as unknown;
7+
8+
9+
//// [privateFieldAssignabilityFromUnknown.js]
10+
"use strict";
11+
var _field;
12+
exports.__esModule = true;
13+
var Class = /** @class */ (function () {
14+
function Class() {
15+
_field.set(this, void 0);
16+
}
17+
return Class;
18+
}());
19+
exports.Class = Class;
20+
_field = new WeakMap();
21+
var task = {};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts ===
2+
export class Class {
3+
>Class : Symbol(Class, Decl(privateFieldAssignabilityFromUnknown.ts, 0, 0))
4+
5+
#field: any
6+
>#field : Symbol(Class.#field, Decl(privateFieldAssignabilityFromUnknown.ts, 0, 20))
7+
}
8+
9+
const task: Class = {} as unknown;
10+
>task : Symbol(task, Decl(privateFieldAssignabilityFromUnknown.ts, 4, 5))
11+
>Class : Symbol(Class, Decl(privateFieldAssignabilityFromUnknown.ts, 0, 0))
12+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts ===
2+
export class Class {
3+
>Class : Class
4+
5+
#field: any
6+
>#field : any
7+
}
8+
9+
const task: Class = {} as unknown;
10+
>task : Class
11+
>{} as unknown : unknown
12+
>{} : {}
13+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class Class {
2+
#field: any
3+
}
4+
5+
const task: Class = {} as unknown;

0 commit comments

Comments
 (0)