Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 ||
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/mappedTypeWithAny.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,18 @@ mappedTypeWithAny.ts(53,5): error TS2322: Type 'string[]' is not assignable to t
type Evolver<T extends Evolvable<any> = any> = {
[key in keyof Partial<T>]: 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<T> = { [k in keyof T]: 1 };
declare function iDontKnow<T>(a: T): [ A<T>, keyof T ];
const something: any = { a: 1, b: 2 };
const [ o, k ] = iDontKnow(something);
const v = o[k]; // ok

30 changes: 30 additions & 0 deletions tests/baselines/reference/mappedTypeWithAny.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ type Evolvable<E extends Evolver> = {
type Evolver<T extends Evolvable<any> = any> = {
[key in keyof Partial<T>]: 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<T> = { [k in keyof T]: 1 };
declare function iDontKnow<T>(a: T): [ A<T>, keyof T ];
const something: any = { a: 1, b: 2 };
const [ o, k ] = iDontKnow(something);
const v = o[k]; // ok


//// [mappedTypeWithAny.js]
Expand All @@ -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]
Expand Down Expand Up @@ -128,3 +146,15 @@ type Evolvable<E extends Evolver> = {
type Evolver<T extends Evolvable<any> = any> = {
[key in keyof Partial<T>]: never;
};
type Obj61203 = {
[k in keyof any]: number;
};
declare const obj61203: Obj61203;
declare const key61203: keyof Obj61203;
type A<T> = {
[k in keyof T]: 1;
};
declare function iDontKnow<T>(a: T): [A<T>, keyof T];
declare const something: any;
declare const o: A<any>, k: string | number | symbol;
declare const v: 1;
50 changes: 50 additions & 0 deletions tests/baselines/reference/mappedTypeWithAny.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,53 @@ type Evolver<T extends Evolvable<any> = 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<T> = { [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<T>(a: T): [ A<T>, 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))

73 changes: 71 additions & 2 deletions tests/baselines/reference/mappedTypeWithAny.types
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
>x4 : ItemMap<any>
Expand Down Expand Up @@ -197,3 +197,72 @@ type Evolver<T extends Evolvable<any> = any> = {
[key in keyof Partial<T>]: 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<T> = { [k in keyof T]: 1 };
>A : A<T>
> : ^^^^

declare function iDontKnow<T>(a: T): [ A<T>, keyof T ];
>iDontKnow : <T>(a: T) => [A<T>, 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<any>
> : ^^^^^^
>k : string | number | symbol
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>iDontKnow(something) : [A<any>, string | number | symbol]
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>iDontKnow : <T>(a: T) => [A<T>, keyof T]
> : ^ ^^ ^^ ^^^^^
>something : any
> : ^^^

const v = o[k]; // ok
>v : 1
> : ^
>o[k] : 1
> : ^
>o : A<any>
> : ^^^^^^
>k : string | number | symbol
> : ^^^^^^^^^^^^^^^^^^^^^^^^

14 changes: 14 additions & 0 deletions tests/cases/conformance/types/mapped/mappedTypeWithAny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,17 @@ type Evolvable<E extends Evolver> = {
type Evolver<T extends Evolvable<any> = any> = {
[key in keyof Partial<T>]: 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<T> = { [k in keyof T]: 1 };
declare function iDontKnow<T>(a: T): [ A<T>, keyof T ];
const something: any = { a: 1, b: 2 };
const [ o, k ] = iDontKnow(something);
const v = o[k]; // ok
Loading