Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't exempt late bound symbols from EPC in presence of string index signature #58190

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/compiler/checker.ts
Expand Up @@ -32543,13 +32543,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
*/
function isKnownProperty(targetType: Type, name: __String, isComparingJsxAttributes: boolean): boolean {
if (targetType.flags & TypeFlags.Object) {
// For backwards compatibility a symbol-named property is satisfied by a string index signature. This
// is incorrect and inconsistent with element access expressions, where it is an error, so eventually
// we should remove this exception.
if (
getPropertyOfObjectType(targetType, name) ||
getApplicableIndexInfoForName(targetType, name) ||
isLateBoundName(name) && getIndexInfoOfType(targetType, stringType) ||
isComparingJsxAttributes && isHyphenatedJsxName(name)
) {
// For JSXAttributes, if the attribute has a hyphenated name, consider that the attribute to be known.
Expand Down
7 changes: 5 additions & 2 deletions tests/baselines/reference/indexSignatures1.errors.txt
Expand Up @@ -68,10 +68,11 @@ indexSignatures1.ts(277,7): error TS2322: Type '"&"' is not assignable to type '
indexSignatures1.ts(281,35): error TS2353: Object literal may only specify known properties, and ''someKey'' does not exist in type 'PseudoDeclaration'.
indexSignatures1.ts(286,7): error TS2322: Type '"two"' is not assignable to type '`/${string}`'.
indexSignatures1.ts(289,7): error TS2322: Type 'number' is not assignable to type 'PathsObject'.
indexSignatures1.ts(311,43): error TS2353: Object literal may only specify known properties, and '[sym]' does not exist in type '{ [key: string]: string; }'.
indexSignatures1.ts(312,43): error TS2353: Object literal may only specify known properties, and '[sym]' does not exist in type '{ [key: number]: string; }'.


==== indexSignatures1.ts (52 errors) ====
==== indexSignatures1.ts (53 errors) ====
// Symbol index signature checking

const sym = Symbol();
Expand Down Expand Up @@ -503,7 +504,9 @@ indexSignatures1.ts(312,43): error TS2353: Object literal may only specify known
const aa: AA = { [sym]: '123' };

const obj1: { [key: symbol]: string } = { [sym]: 'hello '};
const obj2: { [key: string]: string } = { [sym]: 'hello '}; // Permitted for backwards compatibility
const obj2: { [key: string]: string } = { [sym]: 'hello '}; // Error
~~~~~
!!! error TS2353: Object literal may only specify known properties, and '[sym]' does not exist in type '{ [key: string]: string; }'.
const obj3: { [key: number]: string } = { [sym]: 'hello '}; // Error
~~~~~
!!! error TS2353: Object literal may only specify known properties, and '[sym]' does not exist in type '{ [key: number]: string; }'.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/indexSignatures1.js
Expand Up @@ -311,7 +311,7 @@ interface AA {
const aa: AA = { [sym]: '123' };

const obj1: { [key: symbol]: string } = { [sym]: 'hello '};
const obj2: { [key: string]: string } = { [sym]: 'hello '}; // Permitted for backwards compatibility
const obj2: { [key: string]: string } = { [sym]: 'hello '}; // Error
const obj3: { [key: number]: string } = { [sym]: 'hello '}; // Error

// Repro from #45772
Expand Down Expand Up @@ -484,7 +484,7 @@ const a = { [id]: 'test' };
let aid = a[id];
const aa = { [sym]: '123' };
const obj1 = { [sym]: 'hello ' };
const obj2 = { [sym]: 'hello ' }; // Permitted for backwards compatibility
const obj2 = { [sym]: 'hello ' }; // Error
const obj3 = { [sym]: 'hello ' }; // Error


Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/indexSignatures1.symbols
Expand Up @@ -903,7 +903,7 @@ const obj1: { [key: symbol]: string } = { [sym]: 'hello '};
>[sym] : Symbol([sym], Decl(indexSignatures1.ts, 309, 41))
>sym : Symbol(sym, Decl(indexSignatures1.ts, 2, 5))

const obj2: { [key: string]: string } = { [sym]: 'hello '}; // Permitted for backwards compatibility
const obj2: { [key: string]: string } = { [sym]: 'hello '}; // Error
>obj2 : Symbol(obj2, Decl(indexSignatures1.ts, 310, 5))
>key : Symbol(key, Decl(indexSignatures1.ts, 310, 15))
>[sym] : Symbol([sym], Decl(indexSignatures1.ts, 310, 41))
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/indexSignatures1.types
Expand Up @@ -1625,7 +1625,7 @@ const obj1: { [key: symbol]: string } = { [sym]: 'hello '};
>'hello ' : "hello "
> : ^^^^^^^^

const obj2: { [key: string]: string } = { [sym]: 'hello '}; // Permitted for backwards compatibility
const obj2: { [key: string]: string } = { [sym]: 'hello '}; // Error
>obj2 : { [key: string]: string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^
>key : string
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/conformance/types/members/indexSignatures1.ts
Expand Up @@ -312,7 +312,7 @@ interface AA {
const aa: AA = { [sym]: '123' };

const obj1: { [key: symbol]: string } = { [sym]: 'hello '};
const obj2: { [key: string]: string } = { [sym]: 'hello '}; // Permitted for backwards compatibility
const obj2: { [key: string]: string } = { [sym]: 'hello '}; // Error
const obj3: { [key: number]: string } = { [sym]: 'hello '}; // Error

// Repro from #45772
Expand Down