diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 75be36474222f..2673da7dcc8bf 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14720,7 +14720,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { cb(getLiteralTypeFromProperty(prop, include)); } if (type.flags & TypeFlags.Any) { - cb(stringType); + forEachType(stringsOnly ? stringType : stringNumberSymbolType, cb); } else { for (const info of getIndexInfosOfType(type)) { @@ -14759,7 +14759,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { function addMemberForKeyType(keyType: Type) { const propNameType = nameType ? instantiateType(nameType, appendTypeMapping(type.mapper, typeParameter, keyType)) : keyType; - forEachType(propNameType, t => addMemberForKeyTypeWorker(keyType, t)); + forEachType(keyType.flags & TypeFlags.Any ? stringNumberSymbolType : propNameType, t => addMemberForKeyTypeWorker(keyType, t)); } function addMemberForKeyTypeWorker(keyType: Type, propNameType: Type) { @@ -14794,10 +14794,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { members.set(propName, prop); } } - else if (isValidIndexKeyType(propNameType) || propNameType.flags & (TypeFlags.Any | TypeFlags.Enum)) { - const indexKeyType = propNameType.flags & (TypeFlags.Any | TypeFlags.String) ? stringType : - propNameType.flags & (TypeFlags.Number | TypeFlags.Enum) ? numberType : - propNameType; + else if (isValidIndexKeyType(propNameType) || propNameType.flags & TypeFlags.Enum) { + const indexKeyType = propNameType.flags & (TypeFlags.Number | TypeFlags.Enum) ? numberType : propNameType; const propType = instantiateType(templateType, appendTypeMapping(type.mapper, typeParameter, keyType)); const modifiersIndexInfo = getApplicableIndexInfo(modifiersType, propNameType); const isReadonly = !!(templateModifiers & MappedTypeModifiers.IncludeReadonly || diff --git a/tests/baselines/reference/mappedTypeWithAny.errors.txt b/tests/baselines/reference/mappedTypeWithAny.errors.txt index f53402fd7fcb4..4652b49a4b741 100644 --- a/tests/baselines/reference/mappedTypeWithAny.errors.txt +++ b/tests/baselines/reference/mappedTypeWithAny.errors.txt @@ -77,4 +77,18 @@ mappedTypeWithAny.ts(53,5): error TS2322: Type 'string[]' is not assignable to t type Evolver = any> = { [key in keyof Partial]: never; }; + + // https://github.com/microsoft/TypeScript/issues/61203 + type Obj61203 = { [k in keyof any]: number }; + declare const obj61203: Obj61203; + declare const key61203: keyof Obj61203; + obj61203[key61203]; // ok + + + // https://github.com/microsoft/TypeScript/issues/61203#issuecomment-2703862148 + type A = { [k in keyof T]: 1 }; + declare function iDontKnow(a: T): [ A, keyof T ]; + const something: any = { a: 1, b: 2 }; + const [ o, k ] = iDontKnow(something); + const v = o[k]; // ok \ No newline at end of file diff --git a/tests/baselines/reference/mappedTypeWithAny.js b/tests/baselines/reference/mappedTypeWithAny.js index a01d8a6653f42..c97401846db45 100644 --- a/tests/baselines/reference/mappedTypeWithAny.js +++ b/tests/baselines/reference/mappedTypeWithAny.js @@ -63,6 +63,20 @@ type Evolvable = { type Evolver = any> = { [key in keyof Partial]: never; }; + +// https://github.com/microsoft/TypeScript/issues/61203 +type Obj61203 = { [k in keyof any]: number }; +declare const obj61203: Obj61203; +declare const key61203: keyof Obj61203; +obj61203[key61203]; // ok + + +// https://github.com/microsoft/TypeScript/issues/61203#issuecomment-2703862148 +type A = { [k in keyof T]: 1 }; +declare function iDontKnow(a: T): [ A, keyof T ]; +const something: any = { a: 1, b: 2 }; +const [ o, k ] = iDontKnow(something); +const v = o[k]; // ok //// [mappedTypeWithAny.js] @@ -79,6 +93,10 @@ function bar(arrayish, objectish, indirectArrayish) { } var abc = stringifyArray(void 0); var def = stringifyPair(void 0); +obj61203[key61203]; // ok +var something = { a: 1, b: 2 }; +var _a = iDontKnow(something), o = _a[0], k = _a[1]; +var v = o[k]; // ok //// [mappedTypeWithAny.d.ts] @@ -128,3 +146,15 @@ type Evolvable = { type Evolver = any> = { [key in keyof Partial]: never; }; +type Obj61203 = { + [k in keyof any]: number; +}; +declare const obj61203: Obj61203; +declare const key61203: keyof Obj61203; +type A = { + [k in keyof T]: 1; +}; +declare function iDontKnow(a: T): [A, keyof T]; +declare const something: any; +declare const o: A, k: string | number | symbol; +declare const v: 1; diff --git a/tests/baselines/reference/mappedTypeWithAny.symbols b/tests/baselines/reference/mappedTypeWithAny.symbols index b52536f518bc4..4b9d8696e34a4 100644 --- a/tests/baselines/reference/mappedTypeWithAny.symbols +++ b/tests/baselines/reference/mappedTypeWithAny.symbols @@ -177,3 +177,53 @@ type Evolver = any> = { }; +// https://github.com/microsoft/TypeScript/issues/61203 +type Obj61203 = { [k in keyof any]: number }; +>Obj61203 : Symbol(Obj61203, Decl(mappedTypeWithAny.ts, 61, 2)) +>k : Symbol(k, Decl(mappedTypeWithAny.ts, 64, 19)) + +declare const obj61203: Obj61203; +>obj61203 : Symbol(obj61203, Decl(mappedTypeWithAny.ts, 65, 13)) +>Obj61203 : Symbol(Obj61203, Decl(mappedTypeWithAny.ts, 61, 2)) + +declare const key61203: keyof Obj61203; +>key61203 : Symbol(key61203, Decl(mappedTypeWithAny.ts, 66, 13)) +>Obj61203 : Symbol(Obj61203, Decl(mappedTypeWithAny.ts, 61, 2)) + +obj61203[key61203]; // ok +>obj61203 : Symbol(obj61203, Decl(mappedTypeWithAny.ts, 65, 13)) +>key61203 : Symbol(key61203, Decl(mappedTypeWithAny.ts, 66, 13)) + + +// https://github.com/microsoft/TypeScript/issues/61203#issuecomment-2703862148 +type A = { [k in keyof T]: 1 }; +>A : Symbol(A, Decl(mappedTypeWithAny.ts, 67, 19)) +>T : Symbol(T, Decl(mappedTypeWithAny.ts, 71, 7)) +>k : Symbol(k, Decl(mappedTypeWithAny.ts, 71, 15)) +>T : Symbol(T, Decl(mappedTypeWithAny.ts, 71, 7)) + +declare function iDontKnow(a: T): [ A, keyof T ]; +>iDontKnow : Symbol(iDontKnow, Decl(mappedTypeWithAny.ts, 71, 34)) +>T : Symbol(T, Decl(mappedTypeWithAny.ts, 72, 27)) +>a : Symbol(a, Decl(mappedTypeWithAny.ts, 72, 30)) +>T : Symbol(T, Decl(mappedTypeWithAny.ts, 72, 27)) +>A : Symbol(A, Decl(mappedTypeWithAny.ts, 67, 19)) +>T : Symbol(T, Decl(mappedTypeWithAny.ts, 72, 27)) +>T : Symbol(T, Decl(mappedTypeWithAny.ts, 72, 27)) + +const something: any = { a: 1, b: 2 }; +>something : Symbol(something, Decl(mappedTypeWithAny.ts, 73, 5)) +>a : Symbol(a, Decl(mappedTypeWithAny.ts, 73, 24)) +>b : Symbol(b, Decl(mappedTypeWithAny.ts, 73, 30)) + +const [ o, k ] = iDontKnow(something); +>o : Symbol(o, Decl(mappedTypeWithAny.ts, 74, 7)) +>k : Symbol(k, Decl(mappedTypeWithAny.ts, 74, 10)) +>iDontKnow : Symbol(iDontKnow, Decl(mappedTypeWithAny.ts, 71, 34)) +>something : Symbol(something, Decl(mappedTypeWithAny.ts, 73, 5)) + +const v = o[k]; // ok +>v : Symbol(v, Decl(mappedTypeWithAny.ts, 75, 5)) +>o : Symbol(o, Decl(mappedTypeWithAny.ts, 74, 7)) +>k : Symbol(k, Decl(mappedTypeWithAny.ts, 74, 10)) + diff --git a/tests/baselines/reference/mappedTypeWithAny.types b/tests/baselines/reference/mappedTypeWithAny.types index 587ff68bb42f3..f6cb60e0197eb 100644 --- a/tests/baselines/reference/mappedTypeWithAny.types +++ b/tests/baselines/reference/mappedTypeWithAny.types @@ -24,8 +24,8 @@ declare let x2: { [P in string]: Item }; > : ^^^^^^^^^^^^^^^^^^^^^^ declare let x3: { [P in keyof any]: Item }; ->x3 : { [x: string]: Item; } -> : ^^^^^^^^^^^^^^^^^^^^^^ +>x3 : { [x: string]: Item; [x: number]: Item; [x: symbol]: Item; } +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ declare let x4: ItemMap; >x4 : ItemMap @@ -197,3 +197,72 @@ type Evolver = any> = { [key in keyof Partial]: never; }; +// https://github.com/microsoft/TypeScript/issues/61203 +type Obj61203 = { [k in keyof any]: number }; +>Obj61203 : Obj61203 +> : ^^^^^^^^ + +declare const obj61203: Obj61203; +>obj61203 : Obj61203 +> : ^^^^^^^^ + +declare const key61203: keyof Obj61203; +>key61203 : string | number | symbol +> : ^^^^^^^^^^^^^^^^^^^^^^^^ + +obj61203[key61203]; // ok +>obj61203[key61203] : number +> : ^^^^^^ +>obj61203 : Obj61203 +> : ^^^^^^^^ +>key61203 : string | number | symbol +> : ^^^^^^^^^^^^^^^^^^^^^^^^ + + +// https://github.com/microsoft/TypeScript/issues/61203#issuecomment-2703862148 +type A = { [k in keyof T]: 1 }; +>A : A +> : ^^^^ + +declare function iDontKnow(a: T): [ A, keyof T ]; +>iDontKnow : (a: T) => [A, keyof T] +> : ^ ^^ ^^ ^^^^^ +>a : T +> : ^ + +const something: any = { a: 1, b: 2 }; +>something : any +> : ^^^ +>{ a: 1, b: 2 } : { a: number; b: number; } +> : ^^^^^^^^^^^^^^^^^^^^^^^^^ +>a : number +> : ^^^^^^ +>1 : 1 +> : ^ +>b : number +> : ^^^^^^ +>2 : 2 +> : ^ + +const [ o, k ] = iDontKnow(something); +>o : A +> : ^^^^^^ +>k : string | number | symbol +> : ^^^^^^^^^^^^^^^^^^^^^^^^ +>iDontKnow(something) : [A, string | number | symbol] +> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +>iDontKnow : (a: T) => [A, keyof T] +> : ^ ^^ ^^ ^^^^^ +>something : any +> : ^^^ + +const v = o[k]; // ok +>v : 1 +> : ^ +>o[k] : 1 +> : ^ +>o : A +> : ^^^^^^ +>k : string | number | symbol +> : ^^^^^^^^^^^^^^^^^^^^^^^^ + diff --git a/tests/cases/conformance/types/mapped/mappedTypeWithAny.ts b/tests/cases/conformance/types/mapped/mappedTypeWithAny.ts index 89c28ddafc68f..50d4582939da7 100644 --- a/tests/cases/conformance/types/mapped/mappedTypeWithAny.ts +++ b/tests/cases/conformance/types/mapped/mappedTypeWithAny.ts @@ -63,3 +63,17 @@ type Evolvable = { type Evolver = any> = { [key in keyof Partial]: never; }; + +// https://github.com/microsoft/TypeScript/issues/61203 +type Obj61203 = { [k in keyof any]: number }; +declare const obj61203: Obj61203; +declare const key61203: keyof Obj61203; +obj61203[key61203]; // ok + + +// https://github.com/microsoft/TypeScript/issues/61203#issuecomment-2703862148 +type A = { [k in keyof T]: 1 }; +declare function iDontKnow(a: T): [ A, keyof T ]; +const something: any = { a: 1, b: 2 }; +const [ o, k ] = iDontKnow(something); +const v = o[k]; // ok