diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 961a84d6ea0fe..c17f30c9b4de2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16153,6 +16153,7 @@ namespace ts { if (unmatchedProperty.valueDeclaration && isNamedDeclaration(unmatchedProperty.valueDeclaration) && isPrivateIdentifier(unmatchedProperty.valueDeclaration.name) + && source.symbol && source.symbol.flags & SymbolFlags.Class) { const privateIdentifierDescription = unmatchedProperty.valueDeclaration.name.escapedText; const symbolTableKey = getSymbolNameForPrivateIdentifier(source.symbol, privateIdentifierDescription); diff --git a/tests/baselines/reference/privateFieldAssignabilityFromUnknown.errors.txt b/tests/baselines/reference/privateFieldAssignabilityFromUnknown.errors.txt new file mode 100644 index 0000000000000..22f50eec6ff01 --- /dev/null +++ b/tests/baselines/reference/privateFieldAssignabilityFromUnknown.errors.txt @@ -0,0 +1,16 @@ +tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts(2,3): error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher. +tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts(5,7): error TS2741: Property '#field' is missing in type '{}' but required in type 'Class'. + + +==== tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts (2 errors) ==== + export class Class { + #field: any + ~~~~~~ +!!! error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher. + } + + const task: Class = {} as unknown; + ~~~~ +!!! error TS2741: Property '#field' is missing in type '{}' but required in type 'Class'. +!!! related TS2728 tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts:2:3: '#field' is declared here. + \ No newline at end of file diff --git a/tests/baselines/reference/privateFieldAssignabilityFromUnknown.js b/tests/baselines/reference/privateFieldAssignabilityFromUnknown.js new file mode 100644 index 0000000000000..78e0988d8cfb7 --- /dev/null +++ b/tests/baselines/reference/privateFieldAssignabilityFromUnknown.js @@ -0,0 +1,21 @@ +//// [privateFieldAssignabilityFromUnknown.ts] +export class Class { + #field: any +} + +const task: Class = {} as unknown; + + +//// [privateFieldAssignabilityFromUnknown.js] +"use strict"; +var _field; +exports.__esModule = true; +var Class = /** @class */ (function () { + function Class() { + _field.set(this, void 0); + } + return Class; +}()); +exports.Class = Class; +_field = new WeakMap(); +var task = {}; diff --git a/tests/baselines/reference/privateFieldAssignabilityFromUnknown.symbols b/tests/baselines/reference/privateFieldAssignabilityFromUnknown.symbols new file mode 100644 index 0000000000000..0e1be5720e28f --- /dev/null +++ b/tests/baselines/reference/privateFieldAssignabilityFromUnknown.symbols @@ -0,0 +1,12 @@ +=== tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts === +export class Class { +>Class : Symbol(Class, Decl(privateFieldAssignabilityFromUnknown.ts, 0, 0)) + + #field: any +>#field : Symbol(Class.#field, Decl(privateFieldAssignabilityFromUnknown.ts, 0, 20)) +} + +const task: Class = {} as unknown; +>task : Symbol(task, Decl(privateFieldAssignabilityFromUnknown.ts, 4, 5)) +>Class : Symbol(Class, Decl(privateFieldAssignabilityFromUnknown.ts, 0, 0)) + diff --git a/tests/baselines/reference/privateFieldAssignabilityFromUnknown.types b/tests/baselines/reference/privateFieldAssignabilityFromUnknown.types new file mode 100644 index 0000000000000..90aed258dbcd8 --- /dev/null +++ b/tests/baselines/reference/privateFieldAssignabilityFromUnknown.types @@ -0,0 +1,13 @@ +=== tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts === +export class Class { +>Class : Class + + #field: any +>#field : any +} + +const task: Class = {} as unknown; +>task : Class +>{} as unknown : unknown +>{} : {} + diff --git a/tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts b/tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts new file mode 100644 index 0000000000000..09f8cf460ed29 --- /dev/null +++ b/tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts @@ -0,0 +1,5 @@ +export class Class { + #field: any +} + +const task: Class = {} as unknown;