Skip to content

Allow assignability of non-empty object to generic mapped type #32071

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
merged 3 commits into from
Jul 1, 2019
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
8 changes: 4 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13200,14 +13200,14 @@ namespace ts {
if (!isGenericMappedType(source)) {
const targetConstraint = getConstraintTypeFromMappedType(target);
const sourceKeys = getIndexType(source, /*stringsOnly*/ undefined, /*noIndexSignatures*/ true);
const hasOptionalUnionKeys = modifiers & MappedTypeModifiers.IncludeOptional && targetConstraint.flags & TypeFlags.Union;
const filteredByApplicability = hasOptionalUnionKeys ? filterType(targetConstraint, t => !!isRelatedTo(t, sourceKeys)) : undefined;
const includeOptional = modifiers & MappedTypeModifiers.IncludeOptional;
const filteredByApplicability = includeOptional ? intersectTypes(targetConstraint, sourceKeys) : undefined;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My expectation was that when a mapped type target was instantiated with some T with a constraint like { x: number }, either getConstraintTypeFromMappedType(target) or getIndexType(target) would essentially give me keyof { x: number }. But instead, they both give me a fairly unhelpful index type for a generic object.

So then I thought, “ah, maybe if I use the apparent type of the mapped type,” but in the test case I added, getApparentType(target) === target.

My true intent was just to get a plain object type for that instantiation of that generic mapped type, at which point I think I could just call isRelatedTo(source, plainObjectType).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the constraint is keyof U | "a" | "b", getLiteralTypeFromProperties is going to ignore the U-ness of the keyof U part, while the targetConstraint will keep it. We'd like to keep as a keyof (T extends Entity) is different from a keyof (U extends Entity) and needs to be recognized as such. Rather than the filtering using filterType to calculate filteredByApplicability as we're doing now, it's actually probably best to use intersectTypes(targetConstraint, sourceKeys) since the result is the overlapping keys (be they generic or not). Making the indexed access below with that intersection should then work dandily.

In fact, it seems like this is all the changes needed to fix the issue (from master):

diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts
index 64bf09d8b3..4476214e71 100644
--- a/src/compiler/checker.ts
+++ b/src/compiler/checker.ts
@@ -13200,8 +13200,8 @@ namespace ts {
                         if (!isGenericMappedType(source)) {
                             const targetConstraint = getConstraintTypeFromMappedType(target);
                             const sourceKeys = getIndexType(source, /*stringsOnly*/ undefined, /*noIndexSignatures*/ true);
-                            const hasOptionalUnionKeys = modifiers & MappedTypeModifiers.IncludeOptional && targetConstraint.flags & TypeFlags.Union;
-                            const filteredByApplicability = hasOptionalUnionKeys ? filterType(targetConstraint, t => !!isRelatedTo(t, sourceKeys)) : undefined;
+                            const hasOptionalUnionKeys = modifiers & MappedTypeModifiers.IncludeOptional;
+                            const filteredByApplicability = hasOptionalUnionKeys ? intersectTypes(targetConstraint, sourceKeys) : undefined;
                             // A source type T is related to a target type { [P in Q]: X } if Q is related to keyof T and T[Q] is related to X.
                             // A source type T is related to a target type { [P in Q]?: X } if some constituent Q' of Q is related to keyof T and T[Q'] is related to X.
                             if (hasOptionalUnionKeys

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, fantastic, that’s so much better 🙌

// A source type T is related to a target type { [P in Q]: X } if Q is related to keyof T and T[Q] is related to X.
// A source type T is related to a target type { [P in Q]?: X } if some constituent Q' of Q is related to keyof T and T[Q'] is related to X.
if (hasOptionalUnionKeys
if (includeOptional
? !(filteredByApplicability!.flags & TypeFlags.Never)
: isRelatedTo(targetConstraint, sourceKeys)) {
const indexingType = hasOptionalUnionKeys ? filteredByApplicability! : getTypeParameterFromMappedType(target);
const indexingType = filteredByApplicability || getTypeParameterFromMappedType(target);
const indexedAccessType = getIndexedAccessType(source, indexingType);
const templateType = getTemplateTypeFromMappedType(target);
if (result = isRelatedTo(indexedAccessType, templateType, reportErrors)) {
Expand Down

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions tests/baselines/reference/mappedTypeRelationships.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,10 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS
function f82<T, K1 extends keyof T, K2 extends keyof T[K1]>(t: T, k1: K1, k2: K2): Partial<T[K1][K2]> {
return t[k1][k2];
}

// #31070
type Numeric<T> = { [K in keyof T]?: number };
function f90<T extends { x: number }>() {
const n: Numeric<T> = { x: 1 };
}

15 changes: 15 additions & 0 deletions tests/baselines/reference/mappedTypeRelationships.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ function f81<T, K extends keyof T>(t: T, k: K): Partial<T[K]> {
function f82<T, K1 extends keyof T, K2 extends keyof T[K1]>(t: T, k1: K1, k2: K2): Partial<T[K1][K2]> {
return t[k1][k2];
}

// #31070
type Numeric<T> = { [K in keyof T]?: number };
function f90<T extends { x: number }>() {
const n: Numeric<T> = { x: 1 };
}


//// [mappedTypeRelationships.js]
Expand Down Expand Up @@ -310,6 +316,9 @@ function f81(t, k) {
function f82(t, k1, k2) {
return t[k1][k2];
}
function f90() {
var n = { x: 1 };
}


//// [mappedTypeRelationships.d.ts]
Expand Down Expand Up @@ -393,3 +402,9 @@ declare function f76<T, U extends T, K extends keyof T>(x: {
declare function f80<T>(t: T): Partial<T>;
declare function f81<T, K extends keyof T>(t: T, k: K): Partial<T[K]>;
declare function f82<T, K1 extends keyof T, K2 extends keyof T[K1]>(t: T, k1: K1, k2: K2): Partial<T[K1][K2]>;
declare type Numeric<T> = {
[K in keyof T]?: number;
};
declare function f90<T extends {
x: number;
}>(): void;
19 changes: 19 additions & 0 deletions tests/baselines/reference/mappedTypeRelationships.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -805,3 +805,22 @@ function f82<T, K1 extends keyof T, K2 extends keyof T[K1]>(t: T, k1: K1, k2: K2
>k2 : Symbol(k2, Decl(mappedTypeRelationships.ts, 178, 73))
}

// #31070
type Numeric<T> = { [K in keyof T]?: number };
>Numeric : Symbol(Numeric, Decl(mappedTypeRelationships.ts, 180, 1))
>T : Symbol(T, Decl(mappedTypeRelationships.ts, 183, 13))
>K : Symbol(K, Decl(mappedTypeRelationships.ts, 183, 21))
>T : Symbol(T, Decl(mappedTypeRelationships.ts, 183, 13))

function f90<T extends { x: number }>() {
>f90 : Symbol(f90, Decl(mappedTypeRelationships.ts, 183, 46))
>T : Symbol(T, Decl(mappedTypeRelationships.ts, 184, 13))
>x : Symbol(x, Decl(mappedTypeRelationships.ts, 184, 24))

const n: Numeric<T> = { x: 1 };
>n : Symbol(n, Decl(mappedTypeRelationships.ts, 185, 9))
>Numeric : Symbol(Numeric, Decl(mappedTypeRelationships.ts, 180, 1))
>T : Symbol(T, Decl(mappedTypeRelationships.ts, 184, 13))
>x : Symbol(x, Decl(mappedTypeRelationships.ts, 185, 27))
}

15 changes: 15 additions & 0 deletions tests/baselines/reference/mappedTypeRelationships.types
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,18 @@ function f82<T, K1 extends keyof T, K2 extends keyof T[K1]>(t: T, k1: K1, k2: K2
>k2 : K2
}

// #31070
type Numeric<T> = { [K in keyof T]?: number };
>Numeric : Numeric<T>

function f90<T extends { x: number }>() {
>f90 : <T extends { x: number; }>() => void
>x : number

const n: Numeric<T> = { x: 1 };
>n : Numeric<T>
>{ x: 1 } : { x: number; }
>x : number
>1 : 1
}

Loading