diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d402f344d60eb..5e26b37b61ae0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8359,30 +8359,45 @@ namespace ts { return false; } + function isMappedTypeToNever(type: Type) { + return getObjectFlags(type) & ObjectFlags.Mapped && getTemplateTypeFromMappedType(type as MappedType) === neverType; + } + // Transform an indexed access to a simpler form, if possible. Return the simpler form, or return // undefined if no transformation is possible. function getSimplifiedIndexedAccessType(type: IndexedAccessType): Type { const objectType = type.objectType; - // Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or - // more object types with only a string index signature, e.g. '(U & V & { [x: string]: D })[K]', return a - // transformed type of the form '(U & V)[K] | D'. This allows us to properly reason about higher order indexed - // access types with default property values as expressed by D. - if (objectType.flags & TypeFlags.Intersection && isGenericObjectType(objectType) && some((objectType).types, isStringIndexOnlyType)) { - const regularTypes: Type[] = []; - const stringIndexTypes: Type[] = []; - for (const t of (objectType).types) { - if (isStringIndexOnlyType(t)) { - stringIndexTypes.push(getIndexTypeOfType(t, IndexKind.String)); - } - else { - regularTypes.push(t); + if (objectType.flags & TypeFlags.Intersection && isGenericObjectType(objectType)) { + // Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or + // more object types with only a string index signature, e.g. '(U & V & { [x: string]: D })[K]', return a + // transformed type of the form '(U & V)[K] | D'. This allows us to properly reason about higher order indexed + // access types with default property values as expressed by D. + if (some((objectType).types, isStringIndexOnlyType)) { + const regularTypes: Type[] = []; + const stringIndexTypes: Type[] = []; + for (const t of (objectType).types) { + if (isStringIndexOnlyType(t)) { + stringIndexTypes.push(getIndexTypeOfType(t, IndexKind.String)); + } + else { + regularTypes.push(t); + } } + return getUnionType([ + getIndexedAccessType(getIntersectionType(regularTypes), type.indexType), + getIntersectionType(stringIndexTypes) + ]); + } + // Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or + // more mapped types with a template type `never`, '(U & V & { [P in T]: never })[K]', return a + // transformed type that removes the never-mapped type: '(U & V)[K]'. This mirrors what would happen + // eventually anyway, but it easier to reason about. + if (some((objectType).types, isMappedTypeToNever)) { + const nonNeverTypes = filter((objectType).types, t => !isMappedTypeToNever(t)); + return getIndexedAccessType(getIntersectionType(nonNeverTypes), type.indexType); } - return getUnionType([ - getIndexedAccessType(getIntersectionType(regularTypes), type.indexType), - getIntersectionType(stringIndexTypes) - ]); } + // If the object type is a mapped type { [P in K]: E }, where K is generic, instantiate E using a mapper // that substitutes the index type for P. For example, for an index access { [P in K]: Box }[X], we // construct the type Box. diff --git a/tests/baselines/reference/indexedAccessRetainsIndexSignature.js b/tests/baselines/reference/indexedAccessRetainsIndexSignature.js new file mode 100644 index 0000000000000..1b77b95527bec --- /dev/null +++ b/tests/baselines/reference/indexedAccessRetainsIndexSignature.js @@ -0,0 +1,15 @@ +//// [indexedAccessRetainsIndexSignature.ts] +type Diff = + ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T] +type Omit = Pick> +type Omit1 = Pick>; +// is in fact an equivalent of + +type Omit2 = {[P in Diff]: T[P]}; + +type O = Omit<{ a: number, b: string }, 'a'> +const o: O = { b: '' } + + +//// [indexedAccessRetainsIndexSignature.js] +var o = { b: '' }; diff --git a/tests/baselines/reference/indexedAccessRetainsIndexSignature.symbols b/tests/baselines/reference/indexedAccessRetainsIndexSignature.symbols new file mode 100644 index 0000000000000..9858f6fc16209 --- /dev/null +++ b/tests/baselines/reference/indexedAccessRetainsIndexSignature.symbols @@ -0,0 +1,62 @@ +=== tests/cases/compiler/indexedAccessRetainsIndexSignature.ts === +type Diff = +>Diff : Symbol(Diff, Decl(indexedAccessRetainsIndexSignature.ts, 0, 0)) +>T : Symbol(T, Decl(indexedAccessRetainsIndexSignature.ts, 0, 10)) +>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 0, 27)) + + ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T] +>P : Symbol(P, Decl(indexedAccessRetainsIndexSignature.ts, 1, 8)) +>T : Symbol(T, Decl(indexedAccessRetainsIndexSignature.ts, 0, 10)) +>P : Symbol(P, Decl(indexedAccessRetainsIndexSignature.ts, 1, 8)) +>P : Symbol(P, Decl(indexedAccessRetainsIndexSignature.ts, 1, 26)) +>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 0, 27)) +>x : Symbol(x, Decl(indexedAccessRetainsIndexSignature.ts, 1, 48)) +>T : Symbol(T, Decl(indexedAccessRetainsIndexSignature.ts, 0, 10)) + +type Omit = Pick> +>Omit : Symbol(Omit, Decl(indexedAccessRetainsIndexSignature.ts, 1, 71)) +>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 2, 10)) +>K : Symbol(K, Decl(indexedAccessRetainsIndexSignature.ts, 2, 12)) +>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 2, 10)) +>Pick : Symbol(Pick, Decl(lib.d.ts, --, --)) +>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 2, 10)) +>Diff : Symbol(Diff, Decl(indexedAccessRetainsIndexSignature.ts, 0, 0)) +>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 2, 10)) +>K : Symbol(K, Decl(indexedAccessRetainsIndexSignature.ts, 2, 12)) + +type Omit1 = Pick>; +>Omit1 : Symbol(Omit1, Decl(indexedAccessRetainsIndexSignature.ts, 2, 59)) +>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 3, 11)) +>K : Symbol(K, Decl(indexedAccessRetainsIndexSignature.ts, 3, 13)) +>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 3, 11)) +>Pick : Symbol(Pick, Decl(lib.d.ts, --, --)) +>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 3, 11)) +>Diff : Symbol(Diff, Decl(indexedAccessRetainsIndexSignature.ts, 0, 0)) +>U : Symbol(U, Decl(indexedAccessRetainsIndexSignature.ts, 3, 11)) +>K : Symbol(K, Decl(indexedAccessRetainsIndexSignature.ts, 3, 13)) + +// is in fact an equivalent of + +type Omit2 = {[P in Diff]: T[P]}; +>Omit2 : Symbol(Omit2, Decl(indexedAccessRetainsIndexSignature.ts, 3, 61)) +>T : Symbol(T, Decl(indexedAccessRetainsIndexSignature.ts, 6, 11)) +>K : Symbol(K, Decl(indexedAccessRetainsIndexSignature.ts, 6, 13)) +>T : Symbol(T, Decl(indexedAccessRetainsIndexSignature.ts, 6, 11)) +>P : Symbol(P, Decl(indexedAccessRetainsIndexSignature.ts, 6, 37)) +>Diff : Symbol(Diff, Decl(indexedAccessRetainsIndexSignature.ts, 0, 0)) +>T : Symbol(T, Decl(indexedAccessRetainsIndexSignature.ts, 6, 11)) +>K : Symbol(K, Decl(indexedAccessRetainsIndexSignature.ts, 6, 13)) +>T : Symbol(T, Decl(indexedAccessRetainsIndexSignature.ts, 6, 11)) +>P : Symbol(P, Decl(indexedAccessRetainsIndexSignature.ts, 6, 37)) + +type O = Omit<{ a: number, b: string }, 'a'> +>O : Symbol(O, Decl(indexedAccessRetainsIndexSignature.ts, 6, 67)) +>Omit : Symbol(Omit, Decl(indexedAccessRetainsIndexSignature.ts, 1, 71)) +>a : Symbol(a, Decl(indexedAccessRetainsIndexSignature.ts, 8, 15)) +>b : Symbol(b, Decl(indexedAccessRetainsIndexSignature.ts, 8, 26)) + +const o: O = { b: '' } +>o : Symbol(o, Decl(indexedAccessRetainsIndexSignature.ts, 9, 5)) +>O : Symbol(O, Decl(indexedAccessRetainsIndexSignature.ts, 6, 67)) +>b : Symbol(b, Decl(indexedAccessRetainsIndexSignature.ts, 9, 14)) + diff --git a/tests/baselines/reference/indexedAccessRetainsIndexSignature.types b/tests/baselines/reference/indexedAccessRetainsIndexSignature.types new file mode 100644 index 0000000000000..3e3f52f7ca6e5 --- /dev/null +++ b/tests/baselines/reference/indexedAccessRetainsIndexSignature.types @@ -0,0 +1,64 @@ +=== tests/cases/compiler/indexedAccessRetainsIndexSignature.ts === +type Diff = +>Diff : ({ [P in T]: P; } & { [P in U]: never; } & { [x: string]: never; })[T] +>T : T +>U : U + + ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T] +>P : P +>T : T +>P : P +>P : P +>U : U +>x : string +>T : T + +type Omit = Pick> +>Omit : Pick +>U : U +>K : K +>U : U +>Pick : Pick +>U : U +>Diff : ({ [P in T]: P; } & { [P in U]: never; } & { [x: string]: never; })[T] +>U : U +>K : K + +type Omit1 = Pick>; +>Omit1 : Pick +>U : U +>K : K +>U : U +>Pick : Pick +>U : U +>Diff : ({ [P in T]: P; } & { [P in U]: never; } & { [x: string]: never; })[T] +>U : U +>K : K + +// is in fact an equivalent of + +type Omit2 = {[P in Diff]: T[P]}; +>Omit2 : Omit2 +>T : T +>K : K +>T : T +>P : P +>Diff : ({ [P in T]: P; } & { [P in U]: never; } & { [x: string]: never; })[T] +>T : T +>K : K +>T : T +>P : P + +type O = Omit<{ a: number, b: string }, 'a'> +>O : Pick<{ a: number; b: string; }, "b"> +>Omit : Pick +>a : number +>b : string + +const o: O = { b: '' } +>o : Pick<{ a: number; b: string; }, "b"> +>O : Pick<{ a: number; b: string; }, "b"> +>{ b: '' } : { b: string; } +>b : string +>'' : "" + diff --git a/tests/cases/compiler/indexedAccessRetainsIndexSignature.ts b/tests/cases/compiler/indexedAccessRetainsIndexSignature.ts new file mode 100644 index 0000000000000..d23cbe87ddf87 --- /dev/null +++ b/tests/cases/compiler/indexedAccessRetainsIndexSignature.ts @@ -0,0 +1,10 @@ +type Diff = + ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T] +type Omit = Pick> +type Omit1 = Pick>; +// is in fact an equivalent of + +type Omit2 = {[P in Diff]: T[P]}; + +type O = Omit<{ a: number, b: string }, 'a'> +const o: O = { b: '' }