Skip to content

Preserve readonly on mapped index signatures #55541

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

Merged
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
5 changes: 4 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13589,7 +13589,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
propNameType.flags & (TypeFlags.Number | TypeFlags.Enum) ? numberType :
propNameType;
const propType = instantiateType(templateType, appendTypeMapping(type.mapper, typeParameter, keyType));
const indexInfo = createIndexInfo(indexKeyType, propType, !!(templateModifiers & MappedTypeModifiers.IncludeReadonly));
const modifiersIndexInfo = getApplicableIndexInfo(modifiersType, propNameType);
const isReadonly = !!(templateModifiers & MappedTypeModifiers.IncludeReadonly ||
!(templateModifiers & MappedTypeModifiers.ExcludeReadonly) && modifiersIndexInfo?.isReadonly);
const indexInfo = createIndexInfo(indexKeyType, propType, isReadonly);
indexInfos = appendIndexInfo(indexInfos, indexInfo, /*union*/ true);
}
}
Expand Down
210 changes: 210 additions & 0 deletions tests/baselines/reference/mappedTypeIndexSignatureModifiers.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
//// [tests/cases/conformance/types/mapped/mappedTypeIndexSignatureModifiers.ts] ////

=== mappedTypeIndexSignatureModifiers.ts ===
// https://github.com/microsoft/TypeScript/issues/14295

interface Obj1 {
>Obj1 : Symbol(Obj1, Decl(mappedTypeIndexSignatureModifiers.ts, 0, 0))

readonly [key: string]: string;
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 3, 14))
}
type Res1 = Pick<Obj1, keyof Obj1>
>Res1 : Symbol(Res1, Decl(mappedTypeIndexSignatureModifiers.ts, 4, 1))
>Pick : Symbol(Pick, Decl(lib.es5.d.ts, --, --))
>Obj1 : Symbol(Obj1, Decl(mappedTypeIndexSignatureModifiers.ts, 0, 0))
>Obj1 : Symbol(Obj1, Decl(mappedTypeIndexSignatureModifiers.ts, 0, 0))

interface Obj2 {
>Obj2 : Symbol(Obj2, Decl(mappedTypeIndexSignatureModifiers.ts, 5, 34))

concreteProp: 'hello';
>concreteProp : Symbol(Obj2.concreteProp, Decl(mappedTypeIndexSignatureModifiers.ts, 7, 16))

readonly [key: string]: string;
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 9, 14))
}
type Res2 = Pick<Obj2, keyof Obj2>
>Res2 : Symbol(Res2, Decl(mappedTypeIndexSignatureModifiers.ts, 10, 1))
>Pick : Symbol(Pick, Decl(lib.es5.d.ts, --, --))
>Obj2 : Symbol(Obj2, Decl(mappedTypeIndexSignatureModifiers.ts, 5, 34))
>Obj2 : Symbol(Obj2, Decl(mappedTypeIndexSignatureModifiers.ts, 5, 34))

interface Obj3 {
>Obj3 : Symbol(Obj3, Decl(mappedTypeIndexSignatureModifiers.ts, 11, 34))

readonly [key: string]: string;
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 14, 14))

readonly [key: number]: 'foo';
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 15, 14))
}
type Res3 = Pick<Obj3, keyof Obj3>
>Res3 : Symbol(Res3, Decl(mappedTypeIndexSignatureModifiers.ts, 16, 1))
>Pick : Symbol(Pick, Decl(lib.es5.d.ts, --, --))
>Obj3 : Symbol(Obj3, Decl(mappedTypeIndexSignatureModifiers.ts, 11, 34))
>Obj3 : Symbol(Obj3, Decl(mappedTypeIndexSignatureModifiers.ts, 11, 34))

interface Obj4 {
>Obj4 : Symbol(Obj4, Decl(mappedTypeIndexSignatureModifiers.ts, 17, 34))

[key: string]: string;
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 20, 5))

readonly [key: number]: 'foo';
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 21, 14))
}
type Res4 = Pick<Obj4, keyof Obj4>
>Res4 : Symbol(Res4, Decl(mappedTypeIndexSignatureModifiers.ts, 22, 1))
>Pick : Symbol(Pick, Decl(lib.es5.d.ts, --, --))
>Obj4 : Symbol(Obj4, Decl(mappedTypeIndexSignatureModifiers.ts, 17, 34))
>Obj4 : Symbol(Obj4, Decl(mappedTypeIndexSignatureModifiers.ts, 17, 34))

interface Obj5 {
>Obj5 : Symbol(Obj5, Decl(mappedTypeIndexSignatureModifiers.ts, 23, 34))

readonly [key: string]: string;
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 26, 14))

[key: number]: 'foo';
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 27, 5))
}
type Res5 = Pick<Obj5, keyof Obj5>
>Res5 : Symbol(Res5, Decl(mappedTypeIndexSignatureModifiers.ts, 28, 1))
>Pick : Symbol(Pick, Decl(lib.es5.d.ts, --, --))
>Obj5 : Symbol(Obj5, Decl(mappedTypeIndexSignatureModifiers.ts, 23, 34))
>Obj5 : Symbol(Obj5, Decl(mappedTypeIndexSignatureModifiers.ts, 23, 34))

type Identity<T> = { [P in keyof T]: T[P]; }
>Identity : Symbol(Identity, Decl(mappedTypeIndexSignatureModifiers.ts, 29, 34))
>T : Symbol(T, Decl(mappedTypeIndexSignatureModifiers.ts, 31, 14))
>P : Symbol(P, Decl(mappedTypeIndexSignatureModifiers.ts, 31, 22))
>T : Symbol(T, Decl(mappedTypeIndexSignatureModifiers.ts, 31, 14))
>T : Symbol(T, Decl(mappedTypeIndexSignatureModifiers.ts, 31, 14))
>P : Symbol(P, Decl(mappedTypeIndexSignatureModifiers.ts, 31, 22))

interface Obj6 {
>Obj6 : Symbol(Obj6, Decl(mappedTypeIndexSignatureModifiers.ts, 31, 44))

readonly [key: `wow${string}`]: 'foo';
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 34, 14))
}
type Res6 = Identity<Obj6>
>Res6 : Symbol(Res6, Decl(mappedTypeIndexSignatureModifiers.ts, 35, 1))
>Identity : Symbol(Identity, Decl(mappedTypeIndexSignatureModifiers.ts, 29, 34))
>Obj6 : Symbol(Obj6, Decl(mappedTypeIndexSignatureModifiers.ts, 31, 44))

interface Obj7 {
>Obj7 : Symbol(Obj7, Decl(mappedTypeIndexSignatureModifiers.ts, 36, 26))

[key: string]: string;
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 39, 5))

readonly [key: `wow${string}`]: 'foo';
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 40, 14))
}
type Res7 = Identity<Obj7>
>Res7 : Symbol(Res7, Decl(mappedTypeIndexSignatureModifiers.ts, 41, 1))
>Identity : Symbol(Identity, Decl(mappedTypeIndexSignatureModifiers.ts, 29, 34))
>Obj7 : Symbol(Obj7, Decl(mappedTypeIndexSignatureModifiers.ts, 36, 26))

interface Obj8 {
>Obj8 : Symbol(Obj8, Decl(mappedTypeIndexSignatureModifiers.ts, 42, 26))

readonly [key: string]: string;
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 45, 14))

[key: `wow${string}`]: 'foo';
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 46, 5))
}
type Res8 = Identity<Obj8>
>Res8 : Symbol(Res8, Decl(mappedTypeIndexSignatureModifiers.ts, 47, 1))
>Identity : Symbol(Identity, Decl(mappedTypeIndexSignatureModifiers.ts, 29, 34))
>Obj8 : Symbol(Obj8, Decl(mappedTypeIndexSignatureModifiers.ts, 42, 26))

type StrippingPick<T, K extends keyof T> = { -readonly [P in K]: T[P]; }
>StrippingPick : Symbol(StrippingPick, Decl(mappedTypeIndexSignatureModifiers.ts, 48, 26))
>T : Symbol(T, Decl(mappedTypeIndexSignatureModifiers.ts, 50, 19))
>K : Symbol(K, Decl(mappedTypeIndexSignatureModifiers.ts, 50, 21))
>T : Symbol(T, Decl(mappedTypeIndexSignatureModifiers.ts, 50, 19))
>P : Symbol(P, Decl(mappedTypeIndexSignatureModifiers.ts, 50, 56))
>K : Symbol(K, Decl(mappedTypeIndexSignatureModifiers.ts, 50, 21))
>T : Symbol(T, Decl(mappedTypeIndexSignatureModifiers.ts, 50, 19))
>P : Symbol(P, Decl(mappedTypeIndexSignatureModifiers.ts, 50, 56))

interface Obj9 {
>Obj9 : Symbol(Obj9, Decl(mappedTypeIndexSignatureModifiers.ts, 50, 72))

readonly [key: string]: string;
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 53, 14))
}
type Res9 = StrippingPick<Obj9, keyof Obj9>
>Res9 : Symbol(Res9, Decl(mappedTypeIndexSignatureModifiers.ts, 54, 1))
>StrippingPick : Symbol(StrippingPick, Decl(mappedTypeIndexSignatureModifiers.ts, 48, 26))
>Obj9 : Symbol(Obj9, Decl(mappedTypeIndexSignatureModifiers.ts, 50, 72))
>Obj9 : Symbol(Obj9, Decl(mappedTypeIndexSignatureModifiers.ts, 50, 72))

interface Obj10 {
>Obj10 : Symbol(Obj10, Decl(mappedTypeIndexSignatureModifiers.ts, 55, 43))

readonly [key: string]: string;
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 58, 14))

readonly [key: number]: 'foo';
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 59, 14))
}
type Res10 = StrippingPick<Obj10, keyof Obj10>
>Res10 : Symbol(Res10, Decl(mappedTypeIndexSignatureModifiers.ts, 60, 1))
>StrippingPick : Symbol(StrippingPick, Decl(mappedTypeIndexSignatureModifiers.ts, 48, 26))
>Obj10 : Symbol(Obj10, Decl(mappedTypeIndexSignatureModifiers.ts, 55, 43))
>Obj10 : Symbol(Obj10, Decl(mappedTypeIndexSignatureModifiers.ts, 55, 43))

interface Obj11 {
>Obj11 : Symbol(Obj11, Decl(mappedTypeIndexSignatureModifiers.ts, 61, 46))

[key: string]: string;
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 64, 5))

readonly [key: number]: 'foo';
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 65, 14))
}
type Res11 = StrippingPick<Obj11, keyof Obj11>
>Res11 : Symbol(Res11, Decl(mappedTypeIndexSignatureModifiers.ts, 66, 1))
>StrippingPick : Symbol(StrippingPick, Decl(mappedTypeIndexSignatureModifiers.ts, 48, 26))
>Obj11 : Symbol(Obj11, Decl(mappedTypeIndexSignatureModifiers.ts, 61, 46))
>Obj11 : Symbol(Obj11, Decl(mappedTypeIndexSignatureModifiers.ts, 61, 46))

interface Obj12 {
>Obj12 : Symbol(Obj12, Decl(mappedTypeIndexSignatureModifiers.ts, 67, 46))

readonly [key: string]: string;
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 70, 14))

[key: number]: 'foo';
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 71, 5))
}
type Res12 = StrippingPick<Obj12, keyof Obj12>
>Res12 : Symbol(Res12, Decl(mappedTypeIndexSignatureModifiers.ts, 72, 1))
>StrippingPick : Symbol(StrippingPick, Decl(mappedTypeIndexSignatureModifiers.ts, 48, 26))
>Obj12 : Symbol(Obj12, Decl(mappedTypeIndexSignatureModifiers.ts, 67, 46))
>Obj12 : Symbol(Obj12, Decl(mappedTypeIndexSignatureModifiers.ts, 67, 46))

type StrippingIdentity<T> = { -readonly [P in keyof T]: T[P]; }
>StrippingIdentity : Symbol(StrippingIdentity, Decl(mappedTypeIndexSignatureModifiers.ts, 73, 46))
>T : Symbol(T, Decl(mappedTypeIndexSignatureModifiers.ts, 75, 23))
>P : Symbol(P, Decl(mappedTypeIndexSignatureModifiers.ts, 75, 41))
>T : Symbol(T, Decl(mappedTypeIndexSignatureModifiers.ts, 75, 23))
>T : Symbol(T, Decl(mappedTypeIndexSignatureModifiers.ts, 75, 23))
>P : Symbol(P, Decl(mappedTypeIndexSignatureModifiers.ts, 75, 41))

interface Obj13 {
>Obj13 : Symbol(Obj13, Decl(mappedTypeIndexSignatureModifiers.ts, 75, 63))

readonly [key: `wow${string}`]: 'foo';
>key : Symbol(key, Decl(mappedTypeIndexSignatureModifiers.ts, 78, 14))
}
type Res13 = StrippingIdentity<Obj13>
>Res13 : Symbol(Res13, Decl(mappedTypeIndexSignatureModifiers.ts, 79, 1))
>StrippingIdentity : Symbol(StrippingIdentity, Decl(mappedTypeIndexSignatureModifiers.ts, 73, 46))
>Obj13 : Symbol(Obj13, Decl(mappedTypeIndexSignatureModifiers.ts, 75, 63))

132 changes: 132 additions & 0 deletions tests/baselines/reference/mappedTypeIndexSignatureModifiers.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//// [tests/cases/conformance/types/mapped/mappedTypeIndexSignatureModifiers.ts] ////

=== mappedTypeIndexSignatureModifiers.ts ===
// https://github.com/microsoft/TypeScript/issues/14295

interface Obj1 {
readonly [key: string]: string;
>key : string
}
type Res1 = Pick<Obj1, keyof Obj1>
>Res1 : { readonly [x: string]: string; readonly [x: number]: string; }

interface Obj2 {
concreteProp: 'hello';
>concreteProp : "hello"

readonly [key: string]: string;
>key : string
}
type Res2 = Pick<Obj2, keyof Obj2>
>Res2 : { readonly [x: string]: string; readonly [x: number]: string; }

interface Obj3 {
readonly [key: string]: string;
>key : string

readonly [key: number]: 'foo';
>key : number
}
type Res3 = Pick<Obj3, keyof Obj3>
>Res3 : { readonly [x: string]: string; readonly [x: number]: "foo"; }

interface Obj4 {
[key: string]: string;
>key : string

readonly [key: number]: 'foo';
>key : number
}
type Res4 = Pick<Obj4, keyof Obj4>
>Res4 : { [x: string]: string; readonly [x: number]: "foo"; }

interface Obj5 {
readonly [key: string]: string;
>key : string

[key: number]: 'foo';
>key : number
}
type Res5 = Pick<Obj5, keyof Obj5>
>Res5 : { readonly [x: string]: string; [x: number]: "foo"; }

type Identity<T> = { [P in keyof T]: T[P]; }
>Identity : Identity<T>

interface Obj6 {
readonly [key: `wow${string}`]: 'foo';
>key : `wow${string}`
}
type Res6 = Identity<Obj6>
>Res6 : Identity<Obj6>

interface Obj7 {
[key: string]: string;
>key : string

readonly [key: `wow${string}`]: 'foo';
>key : `wow${string}`
}
type Res7 = Identity<Obj7>
>Res7 : Identity<Obj7>

interface Obj8 {
readonly [key: string]: string;
>key : string

[key: `wow${string}`]: 'foo';
>key : `wow${string}`
}
type Res8 = Identity<Obj8>
>Res8 : Identity<Obj8>

type StrippingPick<T, K extends keyof T> = { -readonly [P in K]: T[P]; }
>StrippingPick : StrippingPick<T, K>

interface Obj9 {
readonly [key: string]: string;
>key : string
}
type Res9 = StrippingPick<Obj9, keyof Obj9>
>Res9 : { [x: string]: string; [x: number]: string; }

interface Obj10 {
readonly [key: string]: string;
>key : string

readonly [key: number]: 'foo';
>key : number
}
type Res10 = StrippingPick<Obj10, keyof Obj10>
>Res10 : { [x: string]: string; [x: number]: "foo"; }

interface Obj11 {
[key: string]: string;
>key : string

readonly [key: number]: 'foo';
>key : number
}
type Res11 = StrippingPick<Obj11, keyof Obj11>
>Res11 : { [x: string]: string; [x: number]: "foo"; }

interface Obj12 {
readonly [key: string]: string;
>key : string

[key: number]: 'foo';
>key : number
}
type Res12 = StrippingPick<Obj12, keyof Obj12>
>Res12 : { [x: string]: string; [x: number]: "foo"; }

type StrippingIdentity<T> = { -readonly [P in keyof T]: T[P]; }
>StrippingIdentity : StrippingIdentity<T>

interface Obj13 {
readonly [key: `wow${string}`]: 'foo';
>key : `wow${string}`
}
type Res13 = StrippingIdentity<Obj13>
>Res13 : StrippingIdentity<Obj13>

2 changes: 1 addition & 1 deletion tests/baselines/reference/mappedTypes1.types
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type T30 = { [P in keyof any]: void };
>T30 : { [x: string]: void; }

type T31 = { [P in keyof string]: void };
>T31 : { [x: number]: void; toString: void; charAt: void; charCodeAt: void; concat: void; indexOf: void; lastIndexOf: void; localeCompare: void; match: void; replace: void; search: void; slice: void; split: void; substring: void; toLowerCase: void; toLocaleLowerCase: void; toUpperCase: void; toLocaleUpperCase: void; trim: void; readonly length: void; substr: void; valueOf: void; }
>T31 : { readonly [x: number]: void; toString: void; charAt: void; charCodeAt: void; concat: void; indexOf: void; lastIndexOf: void; localeCompare: void; match: void; replace: void; search: void; slice: void; split: void; substring: void; toLowerCase: void; toLocaleLowerCase: void; toUpperCase: void; toLocaleUpperCase: void; trim: void; readonly length: void; substr: void; valueOf: void; }

type T32 = { [P in keyof number]: void };
>T32 : { toString: void; toFixed: void; toExponential: void; toPrecision: void; valueOf: void; toLocaleString: void; }
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/staticIndexSignature5.types
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ type TE = keyof typeof B;
>B : typeof B

type TF = Pick<typeof B, number>
>TF : { [x: number]: 42 | 233; }
>TF : { readonly [x: number]: 42 | 233; }
>B : typeof B

type TFI = Pick<I, number>
>TFI : { [x: number]: 42 | 233; }
>TFI : { readonly [x: number]: 42 | 233; }

type TG = Omit<typeof B, number>
>TG : { [x: string]: number; }
>TG : { readonly [x: string]: number; }
>B : typeof B

type TGI = Omit<I, number>
>TGI : { [x: string]: number; }
>TGI : { readonly [x: string]: number; }

Loading