Skip to content

Let assignability checking of generic mapped types decompose intersections on both the source and the target. #27590

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

Closed
wants to merge 1 commit into from
Closed
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
35 changes: 30 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11817,6 +11817,16 @@ namespace ts {
return relation === definitelyAssignableRelation ? undefined : getConstraintOfType(type);
}

function typeRelatedToMappedTypeTemplateConstituent(source: Type, parameter: TypeParameter, templateConstituent: Type, reportErrors: boolean) {
if (templateConstituent.flags & TypeFlags.IndexedAccess &&
(<IndexedAccessType>templateConstituent).indexType === parameter) {
return isRelatedTo(source, (<IndexedAccessType>templateConstituent).objectType, reportErrors);
}
else {
return Ternary.False;
}
}

function structuredTypeRelatedTo(source: Type, target: Type, reportErrors: boolean): Ternary {
const flags = source.flags & target.flags;
if (relation === identityRelation && !(flags & TypeFlags.Object)) {
Expand Down Expand Up @@ -11853,8 +11863,10 @@ namespace ts {
let originalErrorInfo: DiagnosticMessageChain | undefined;
const saveErrorInfo = errorInfo;
if (target.flags & TypeFlags.TypeParameter) {
// A source type { [P in keyof T]: X } is related to a target type T if X is related to T[P].
if (getObjectFlags(source) & ObjectFlags.Mapped && getConstraintTypeFromMappedType(<MappedType>source) === getIndexType(target)) {
// A source type { [P in K]: X } is related to a target type T if keyof T is related to K
// and X is related to T[P].
if (getObjectFlags(source) & ObjectFlags.Mapped &&
isRelatedTo(getIndexType(target), getConstraintTypeFromMappedType(<MappedType>source))) {
if (!(getMappedTypeModifiers(<MappedType>source) & MappedTypeModifiers.IncludeOptional)) {
const templateType = getTemplateTypeFromMappedType(<MappedType>source);
const indexedAccessType = getIndexedAccessType(target, getTypeParameterFromMappedType(<MappedType>source));
Expand Down Expand Up @@ -11904,9 +11916,22 @@ namespace ts {
const template = getTemplateTypeFromMappedType(target);
const modifiers = getMappedTypeModifiers(target);
if (!(modifiers & MappedTypeModifiers.ExcludeOptional)) {
if (template.flags & TypeFlags.IndexedAccess && (<IndexedAccessType>template).objectType === source &&
(<IndexedAccessType>template).indexType === getTypeParameterFromMappedType(target)) {
return Ternary.True;
const parameter = getTypeParameterFromMappedType(target);
if (template.flags & TypeFlags.Intersection) {
// C.f. typeRelatedToEachType
result = Ternary.True;
for (const templateConstituent of (<IntersectionType>template).types) {
result &= typeRelatedToMappedTypeTemplateConstituent(source, parameter, templateConstituent, reportErrors);
if (!result) {
break;
}
}
}
else {
result = typeRelatedToMappedTypeTemplateConstituent(source, parameter, template, reportErrors);
}
if (result) {
return result;
}
// A source type T is related to a target type { [P in keyof T]: X } if T[P] is related to X.
if (!isGenericMappedType(source) && getConstraintTypeFromMappedType(target) === getIndexType(source)) {
Expand Down
4 changes: 4 additions & 0 deletions tests/baselines/reference/conditionalTypes1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(106,5): error TS2
Type 'keyof T' is not assignable to type 'never'.
Type 'string | number | symbol' is not assignable to type 'never'.
Type 'string' is not assignable to type 'never'.
Type 'Pick<T, { [K in keyof T]: T[K] extends Function ? never : K; }[keyof T]>' is not assignable to type 'T'.
tests/cases/conformance/types/conditional/conditionalTypes1.ts(108,5): error TS2322: Type 'Pick<T, { [K in keyof T]: T[K] extends Function ? K : never; }[keyof T]>' is not assignable to type 'Pick<T, { [K in keyof T]: T[K] extends Function ? never : K; }[keyof T]>'.
Type 'T[keyof T] extends Function ? never : keyof T' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
Type 'keyof T' is not assignable to type 'never'.
Type 'Pick<T, { [K in keyof T]: T[K] extends Function ? K : never; }[keyof T]>' is not assignable to type 'T'.
tests/cases/conformance/types/conditional/conditionalTypes1.ts(114,5): error TS2322: Type 'keyof T' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
Type 'string | number | symbol' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
Type 'string' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
Expand Down Expand Up @@ -190,12 +192,14 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS
!!! error TS2322: Type 'keyof T' is not assignable to type 'never'.
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'never'.
!!! error TS2322: Type 'string' is not assignable to type 'never'.
!!! error TS2322: Type 'Pick<T, { [K in keyof T]: T[K] extends Function ? never : K; }[keyof T]>' is not assignable to type 'T'.
z = x;
z = y; // Error
~
!!! error TS2322: Type 'Pick<T, { [K in keyof T]: T[K] extends Function ? K : never; }[keyof T]>' is not assignable to type 'Pick<T, { [K in keyof T]: T[K] extends Function ? never : K; }[keyof T]>'.
!!! error TS2322: Type 'T[keyof T] extends Function ? never : keyof T' is not assignable to type 'T[keyof T] extends Function ? keyof T : never'.
!!! error TS2322: Type 'keyof T' is not assignable to type 'never'.
!!! error TS2322: Type 'Pick<T, { [K in keyof T]: T[K] extends Function ? K : never; }[keyof T]>' is not assignable to type 'T'.
}

function f8<T>(x: keyof T, y: FunctionPropertyNames<T>, z: NonFunctionPropertyNames<T>) {
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/genericMappedTypeIntersections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [genericMappedTypeIntersections.ts]
// Repro for #27484
function makePropsForWrappedComponent<PassthroughProps, ExternalProps, InjectedProps>(
outerProps: Readonly<PassthroughProps & ExternalProps>,
injectedProps: Readonly<InjectedProps>): Readonly<PassthroughProps & InjectedProps> {
return Object.assign({}, injectedProps, outerProps);
}


//// [genericMappedTypeIntersections.js]
// Repro for #27484
function makePropsForWrappedComponent(outerProps, injectedProps) {
return Object.assign({}, injectedProps, outerProps);
}
30 changes: 30 additions & 0 deletions tests/baselines/reference/genericMappedTypeIntersections.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/compiler/genericMappedTypeIntersections.ts ===
// Repro for #27484
function makePropsForWrappedComponent<PassthroughProps, ExternalProps, InjectedProps>(
>makePropsForWrappedComponent : Symbol(makePropsForWrappedComponent, Decl(genericMappedTypeIntersections.ts, 0, 0))
>PassthroughProps : Symbol(PassthroughProps, Decl(genericMappedTypeIntersections.ts, 1, 38))
>ExternalProps : Symbol(ExternalProps, Decl(genericMappedTypeIntersections.ts, 1, 55))
>InjectedProps : Symbol(InjectedProps, Decl(genericMappedTypeIntersections.ts, 1, 70))

outerProps: Readonly<PassthroughProps & ExternalProps>,
>outerProps : Symbol(outerProps, Decl(genericMappedTypeIntersections.ts, 1, 86))
>Readonly : Symbol(Readonly, Decl(lib.es5.d.ts, --, --))
>PassthroughProps : Symbol(PassthroughProps, Decl(genericMappedTypeIntersections.ts, 1, 38))
>ExternalProps : Symbol(ExternalProps, Decl(genericMappedTypeIntersections.ts, 1, 55))

injectedProps: Readonly<InjectedProps>): Readonly<PassthroughProps & InjectedProps> {
>injectedProps : Symbol(injectedProps, Decl(genericMappedTypeIntersections.ts, 2, 59))
>Readonly : Symbol(Readonly, Decl(lib.es5.d.ts, --, --))
>InjectedProps : Symbol(InjectedProps, Decl(genericMappedTypeIntersections.ts, 1, 70))
>Readonly : Symbol(Readonly, Decl(lib.es5.d.ts, --, --))
>PassthroughProps : Symbol(PassthroughProps, Decl(genericMappedTypeIntersections.ts, 1, 38))
>InjectedProps : Symbol(InjectedProps, Decl(genericMappedTypeIntersections.ts, 1, 70))

return Object.assign({}, injectedProps, outerProps);
>Object.assign : Symbol(ObjectConstructor.assign, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>assign : Symbol(ObjectConstructor.assign, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>injectedProps : Symbol(injectedProps, Decl(genericMappedTypeIntersections.ts, 2, 59))
>outerProps : Symbol(outerProps, Decl(genericMappedTypeIntersections.ts, 1, 86))
}

21 changes: 21 additions & 0 deletions tests/baselines/reference/genericMappedTypeIntersections.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/genericMappedTypeIntersections.ts ===
// Repro for #27484
function makePropsForWrappedComponent<PassthroughProps, ExternalProps, InjectedProps>(
>makePropsForWrappedComponent : <PassthroughProps, ExternalProps, InjectedProps>(outerProps: Readonly<PassthroughProps & ExternalProps>, injectedProps: Readonly<InjectedProps>) => Readonly<PassthroughProps & InjectedProps>

outerProps: Readonly<PassthroughProps & ExternalProps>,
>outerProps : Readonly<PassthroughProps & ExternalProps>

injectedProps: Readonly<InjectedProps>): Readonly<PassthroughProps & InjectedProps> {
>injectedProps : Readonly<InjectedProps>

return Object.assign({}, injectedProps, outerProps);
>Object.assign({}, injectedProps, outerProps) : Readonly<InjectedProps> & Readonly<PassthroughProps & ExternalProps>
>Object.assign : { <T, U>(target: T, source: U): T & U; <T, U, V>(target: T, source1: U, source2: V): T & U & V; <T, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W; (target: object, ...sources: any[]): any; }
>Object : ObjectConstructor
>assign : { <T, U>(target: T, source: U): T & U; <T, U, V>(target: T, source1: U, source2: V): T & U & V; <T, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W; (target: object, ...sources: any[]): any; }
>{} : {}
>injectedProps : Readonly<InjectedProps>
>outerProps : Readonly<PassthroughProps & ExternalProps>
}

16 changes: 16 additions & 0 deletions tests/baselines/reference/mappedTypeRelationships.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,37 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(66,5): error TS2
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(72,5): error TS2322: Type 'Partial<T>' is not assignable to type 'T'.
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(78,5): error TS2322: Type 'Partial<Thing>' is not assignable to type 'Partial<T>'.
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(88,5): error TS2322: Type 'Readonly<Thing>' is not assignable to type 'Readonly<T>'.
Type 'Readonly<Thing>' is not assignable to type 'T'.
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(127,5): error TS2322: Type 'Partial<U>' is not assignable to type 'Identity<U>'.
Type 'Partial<U>' is not assignable to type 'U'.
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(143,5): error TS2322: Type '{ [P in keyof T]: T[P]; }' is not assignable to type '{ [P in keyof T]: U[P]; }'.
Type 'T[P]' is not assignable to type 'U[P]'.
Type 'T' is not assignable to type 'U'.
Type '{ [P in keyof T]: T[P]; }' is not assignable to type 'U'.
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(148,5): error TS2322: Type '{ [P in keyof T]: T[P]; }' is not assignable to type '{ [P in keyof U]: U[P]; }'.
Type 'keyof U' is not assignable to type 'keyof T'.
Type 'string | number | symbol' is not assignable to type 'keyof T'.
Type 'string' is not assignable to type 'keyof T'.
Type '{ [P in keyof T]: T[P]; }' is not assignable to type 'U'.
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(153,5): error TS2322: Type '{ [P in K]: T[P]; }' is not assignable to type '{ [P in keyof T]: T[P]; }'.
Type 'keyof T' is not assignable to type 'K'.
Type 'string | number | symbol' is not assignable to type 'K'.
Type 'string' is not assignable to type 'K'.
Type '{ [P in K]: T[P]; }' is not assignable to type 'T'.
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(158,5): error TS2322: Type '{ [P in K]: T[P]; }' is not assignable to type '{ [P in keyof U]: U[P]; }'.
Type 'keyof U' is not assignable to type 'K'.
Type 'string | number | symbol' is not assignable to type 'K'.
Type 'string' is not assignable to type 'K'.
Type '{ [P in K]: T[P]; }' is not assignable to type 'U'.
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(163,5): error TS2322: Type '{ [P in K]: T[P]; }' is not assignable to type '{ [P in keyof T]: U[P]; }'.
Type 'keyof T' is not assignable to type 'K'.
Type 'string | number | symbol' is not assignable to type 'K'.
Type 'string' is not assignable to type 'K'.
Type '{ [P in K]: T[P]; }' is not assignable to type 'U'.
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is not assignable to type '{ [P in K]: U[P]; }'.
Type 'T[P]' is not assignable to type 'U[P]'.
Type 'T' is not assignable to type 'U'.
Type '{ [P in K]: T[P]; }' is not assignable to type 'U'.


==== tests/cases/conformance/types/mapped/mappedTypeRelationships.ts (30 errors) ====
Expand Down Expand Up @@ -209,6 +217,7 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS
y = x; // Error
~
!!! error TS2322: Type 'Readonly<Thing>' is not assignable to type 'Readonly<T>'.
!!! error TS2322: Type 'Readonly<Thing>' is not assignable to type 'T'.
}

type Item = {
Expand Down Expand Up @@ -250,6 +259,7 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS
x = y; // Error
~
!!! error TS2322: Type 'Partial<U>' is not assignable to type 'Identity<U>'.
!!! error TS2322: Type 'Partial<U>' is not assignable to type 'U'.
y = x;
}

Expand All @@ -270,6 +280,7 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS
!!! error TS2322: Type '{ [P in keyof T]: T[P]; }' is not assignable to type '{ [P in keyof T]: U[P]; }'.
!!! error TS2322: Type 'T[P]' is not assignable to type 'U[P]'.
!!! error TS2322: Type 'T' is not assignable to type 'U'.
!!! error TS2322: Type '{ [P in keyof T]: T[P]; }' is not assignable to type 'U'.
}

function f72<T, U extends T>(x: { [P in keyof T]: T[P] }, y: { [P in keyof U]: U[P] }) {
Expand All @@ -280,6 +291,7 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS
!!! error TS2322: Type 'keyof U' is not assignable to type 'keyof T'.
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'keyof T'.
!!! error TS2322: Type 'string' is not assignable to type 'keyof T'.
!!! error TS2322: Type '{ [P in keyof T]: T[P]; }' is not assignable to type 'U'.
}

function f73<T, K extends keyof T>(x: { [P in K]: T[P] }, y: { [P in keyof T]: T[P] }) {
Expand All @@ -290,6 +302,7 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS
!!! error TS2322: Type 'keyof T' is not assignable to type 'K'.
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'K'.
!!! error TS2322: Type 'string' is not assignable to type 'K'.
!!! error TS2322: Type '{ [P in K]: T[P]; }' is not assignable to type 'T'.
}

function f74<T, U extends T, K extends keyof T>(x: { [P in K]: T[P] }, y: { [P in keyof U]: U[P] }) {
Expand All @@ -300,6 +313,7 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS
!!! error TS2322: Type 'keyof U' is not assignable to type 'K'.
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'K'.
!!! error TS2322: Type 'string' is not assignable to type 'K'.
!!! error TS2322: Type '{ [P in K]: T[P]; }' is not assignable to type 'U'.
}

function f75<T, U extends T, K extends keyof T>(x: { [P in K]: T[P] }, y: { [P in keyof T]: U[P] }) {
Expand All @@ -310,6 +324,7 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS
!!! error TS2322: Type 'keyof T' is not assignable to type 'K'.
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'K'.
!!! error TS2322: Type 'string' is not assignable to type 'K'.
!!! error TS2322: Type '{ [P in K]: T[P]; }' is not assignable to type 'U'.
}

function f76<T, U extends T, K extends keyof T>(x: { [P in K]: T[P] }, y: { [P in K]: U[P] }) {
Expand All @@ -319,6 +334,7 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS
!!! error TS2322: Type '{ [P in K]: T[P]; }' is not assignable to type '{ [P in K]: U[P]; }'.
!!! error TS2322: Type 'T[P]' is not assignable to type 'U[P]'.
!!! error TS2322: Type 'T' is not assignable to type 'U'.
!!! error TS2322: Type '{ [P in K]: T[P]; }' is not assignable to type 'U'.
}

function f80<T>(t: T): Partial<T> {
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/mappedTypes5.errors.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
tests/cases/conformance/types/mapped/mappedTypes5.ts(6,9): error TS2322: Type 'Partial<T>' is not assignable to type 'Readonly<T>'.
Type 'Partial<T>' is not assignable to type 'T'.
tests/cases/conformance/types/mapped/mappedTypes5.ts(8,9): error TS2322: Type 'Partial<Readonly<T>>' is not assignable to type 'Readonly<T>'.
Type 'Partial<Readonly<T>>' is not assignable to type 'T'.
tests/cases/conformance/types/mapped/mappedTypes5.ts(9,9): error TS2322: Type 'Readonly<Partial<T>>' is not assignable to type 'Readonly<T>'.
Type 'Readonly<Partial<T>>' is not assignable to type 'T'.
Type 'T[P] | undefined' is not assignable to type 'T[P]'.
Type 'undefined' is not assignable to type 'T[P]'.


==== tests/cases/conformance/types/mapped/mappedTypes5.ts (3 errors) ====
Expand All @@ -12,13 +17,18 @@ tests/cases/conformance/types/mapped/mappedTypes5.ts(9,9): error TS2322: Type 'R
let b1: Readonly<T> = p; // Error
~~
!!! error TS2322: Type 'Partial<T>' is not assignable to type 'Readonly<T>'.
!!! error TS2322: Type 'Partial<T>' is not assignable to type 'T'.
let b2: Readonly<T> = r;
let b3: Readonly<T> = pr; // Error
~~
!!! error TS2322: Type 'Partial<Readonly<T>>' is not assignable to type 'Readonly<T>'.
!!! error TS2322: Type 'Partial<Readonly<T>>' is not assignable to type 'T'.
let b4: Readonly<T> = rp; // Error
~~
!!! error TS2322: Type 'Readonly<Partial<T>>' is not assignable to type 'Readonly<T>'.
!!! error TS2322: Type 'Readonly<Partial<T>>' is not assignable to type 'T'.
!!! error TS2322: Type 'T[P] | undefined' is not assignable to type 'T[P]'.
!!! error TS2322: Type 'undefined' is not assignable to type 'T[P]'.
let c1: Partial<Readonly<T>> = p;
let c2: Partial<Readonly<T>> = r;
let c3: Partial<Readonly<T>> = pr;
Expand Down
7 changes: 7 additions & 0 deletions tests/cases/compiler/genericMappedTypeIntersections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Repro for #27484
// @target: es6
function makePropsForWrappedComponent<PassthroughProps, ExternalProps, InjectedProps>(
outerProps: Readonly<PassthroughProps & ExternalProps>,
injectedProps: Readonly<InjectedProps>): Readonly<PassthroughProps & InjectedProps> {
return Object.assign({}, injectedProps, outerProps);
}