Skip to content

Add missing conditional type relationships #24821

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
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
192 changes: 162 additions & 30 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4064,8 +4064,9 @@ namespace ts {
// Thus, if Foo has a 'string' constraint on its type parameter, T will satisfy it. Substitution
// types disappear upon instantiation (just like type parameters).
export interface SubstitutionType extends InstantiableType {
typeVariable: TypeVariable; // Target type variable
typeVariable: Type; // Target type variable
substitute: Type; // Type to substitute for type parameter
negatedTypes?: Type[]; // Types proven that this type variables _doesn't_ extend
}

export const enum SignatureKind {
Expand Down Expand Up @@ -4152,6 +4153,7 @@ namespace ts {
InferUnionTypes = 1 << 0, // Infer union types for disjoint candidates (otherwise unknownType)
NoDefault = 1 << 1, // Infer unknownType for no inferences (otherwise anyType or emptyObjectType)
AnyDefault = 1 << 2, // Infer anyType for no inferences (otherwise emptyObjectType)
SkipConstraintCheck = 1 << 3, // Skip checking weather the inference is assignable to the constraint (otherwise is replaced with constraint)
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2300,8 +2300,9 @@ declare namespace ts {
resolvedFalseType?: Type;
}
interface SubstitutionType extends InstantiableType {
typeVariable: TypeVariable;
typeVariable: Type;
substitute: Type;
negatedTypes?: Type[];
}
enum SignatureKind {
Call = 0,
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2300,8 +2300,9 @@ declare namespace ts {
resolvedFalseType?: Type;
}
interface SubstitutionType extends InstantiableType {
typeVariable: TypeVariable;
typeVariable: Type;
substitute: Type;
negatedTypes?: Type[];
}
enum SignatureKind {
Call = 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
tests/cases/compiler/conditionalTypeGenericAssignability.ts(3,5): error TS2322: Type '0' is not assignable to type 'Extract<keyof T, string>'.
tests/cases/compiler/conditionalTypeGenericAssignability.ts(7,5): error TS2322: Type '"foo"' is not assignable to type 'Exclude<keyof T, string>'.
tests/cases/compiler/conditionalTypeGenericAssignability.ts(16,5): error TS2322: Type '{ y: { x: T; }; }' is not assignable to type '{ x: T; } extends { x: string; } ? { y: { x: T; }; } : never'.


==== tests/cases/compiler/conditionalTypeGenericAssignability.ts (3 errors) ====
function f1<T extends { foo: unknown; 0: unknown }>(_a: T, b: Extract<keyof T, string>) {
b = "foo"; // succeeds
b = 0; // errors
~
!!! error TS2322: Type '0' is not assignable to type 'Extract<keyof T, string>'.
}

function f2<T extends { foo: unknown; 0: unknown }>(_a: T, b: Exclude<keyof T, string>) {
b = "foo"; // errors
~
!!! error TS2322: Type '"foo"' is not assignable to type 'Exclude<keyof T, string>'.
b = 0; // succeeds
}

function f3<T extends number | string>(
i: T & string,
j: T,
b: { x: T } extends { x: string } ? { y: { x: T } } : never) {
b = { y: { x: i } }; // success
b = { y: { x: j } }; // failure
~
!!! error TS2322: Type '{ y: { x: T; }; }' is not assignable to type '{ x: T; } extends { x: string; } ? { y: { x: T; }; } : never'.
}

34 changes: 34 additions & 0 deletions tests/baselines/reference/conditionalTypeGenericAssignability.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [conditionalTypeGenericAssignability.ts]
function f1<T extends { foo: unknown; 0: unknown }>(_a: T, b: Extract<keyof T, string>) {
b = "foo"; // succeeds
b = 0; // errors
}

function f2<T extends { foo: unknown; 0: unknown }>(_a: T, b: Exclude<keyof T, string>) {
b = "foo"; // errors
b = 0; // succeeds
}

function f3<T extends number | string>(
i: T & string,
j: T,
b: { x: T } extends { x: string } ? { y: { x: T } } : never) {
b = { y: { x: i } }; // success
b = { y: { x: j } }; // failure
}


//// [conditionalTypeGenericAssignability.js]
"use strict";
function f1(_a, b) {
b = "foo"; // succeeds
b = 0; // errors
}
function f2(_a, b) {
b = "foo"; // errors
b = 0; // succeeds
}
function f3(i, j, b) {
b = { y: { x: i } }; // success
b = { y: { x: j } }; // failure
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
=== tests/cases/compiler/conditionalTypeGenericAssignability.ts ===
function f1<T extends { foo: unknown; 0: unknown }>(_a: T, b: Extract<keyof T, string>) {
>f1 : Symbol(f1, Decl(conditionalTypeGenericAssignability.ts, 0, 0))
>T : Symbol(T, Decl(conditionalTypeGenericAssignability.ts, 0, 12))
>foo : Symbol(foo, Decl(conditionalTypeGenericAssignability.ts, 0, 23))
>0 : Symbol(0, Decl(conditionalTypeGenericAssignability.ts, 0, 37))
>_a : Symbol(_a, Decl(conditionalTypeGenericAssignability.ts, 0, 52))
>T : Symbol(T, Decl(conditionalTypeGenericAssignability.ts, 0, 12))
>b : Symbol(b, Decl(conditionalTypeGenericAssignability.ts, 0, 58))
>Extract : Symbol(Extract, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(conditionalTypeGenericAssignability.ts, 0, 12))

b = "foo"; // succeeds
>b : Symbol(b, Decl(conditionalTypeGenericAssignability.ts, 0, 58))

b = 0; // errors
>b : Symbol(b, Decl(conditionalTypeGenericAssignability.ts, 0, 58))
}

function f2<T extends { foo: unknown; 0: unknown }>(_a: T, b: Exclude<keyof T, string>) {
>f2 : Symbol(f2, Decl(conditionalTypeGenericAssignability.ts, 3, 1))
>T : Symbol(T, Decl(conditionalTypeGenericAssignability.ts, 5, 12))
>foo : Symbol(foo, Decl(conditionalTypeGenericAssignability.ts, 5, 23))
>0 : Symbol(0, Decl(conditionalTypeGenericAssignability.ts, 5, 37))
>_a : Symbol(_a, Decl(conditionalTypeGenericAssignability.ts, 5, 52))
>T : Symbol(T, Decl(conditionalTypeGenericAssignability.ts, 5, 12))
>b : Symbol(b, Decl(conditionalTypeGenericAssignability.ts, 5, 58))
>Exclude : Symbol(Exclude, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(conditionalTypeGenericAssignability.ts, 5, 12))

b = "foo"; // errors
>b : Symbol(b, Decl(conditionalTypeGenericAssignability.ts, 5, 58))

b = 0; // succeeds
>b : Symbol(b, Decl(conditionalTypeGenericAssignability.ts, 5, 58))
}

function f3<T extends number | string>(
>f3 : Symbol(f3, Decl(conditionalTypeGenericAssignability.ts, 8, 1))
>T : Symbol(T, Decl(conditionalTypeGenericAssignability.ts, 10, 12))

i: T & string,
>i : Symbol(i, Decl(conditionalTypeGenericAssignability.ts, 10, 39))
>T : Symbol(T, Decl(conditionalTypeGenericAssignability.ts, 10, 12))

j: T,
>j : Symbol(j, Decl(conditionalTypeGenericAssignability.ts, 11, 18))
>T : Symbol(T, Decl(conditionalTypeGenericAssignability.ts, 10, 12))

b: { x: T } extends { x: string } ? { y: { x: T } } : never) {
>b : Symbol(b, Decl(conditionalTypeGenericAssignability.ts, 12, 9))
>x : Symbol(x, Decl(conditionalTypeGenericAssignability.ts, 13, 8))
>T : Symbol(T, Decl(conditionalTypeGenericAssignability.ts, 10, 12))
>x : Symbol(x, Decl(conditionalTypeGenericAssignability.ts, 13, 25))
>y : Symbol(y, Decl(conditionalTypeGenericAssignability.ts, 13, 41))
>x : Symbol(x, Decl(conditionalTypeGenericAssignability.ts, 13, 46))
>T : Symbol(T, Decl(conditionalTypeGenericAssignability.ts, 10, 12))

b = { y: { x: i } }; // success
>b : Symbol(b, Decl(conditionalTypeGenericAssignability.ts, 12, 9))
>y : Symbol(y, Decl(conditionalTypeGenericAssignability.ts, 14, 9))
>x : Symbol(x, Decl(conditionalTypeGenericAssignability.ts, 14, 14))
>i : Symbol(i, Decl(conditionalTypeGenericAssignability.ts, 10, 39))

b = { y: { x: j } }; // failure
>b : Symbol(b, Decl(conditionalTypeGenericAssignability.ts, 12, 9))
>y : Symbol(y, Decl(conditionalTypeGenericAssignability.ts, 15, 9))
>x : Symbol(x, Decl(conditionalTypeGenericAssignability.ts, 15, 14))
>j : Symbol(j, Decl(conditionalTypeGenericAssignability.ts, 11, 18))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
=== tests/cases/compiler/conditionalTypeGenericAssignability.ts ===
function f1<T extends { foo: unknown; 0: unknown }>(_a: T, b: Extract<keyof T, string>) {
>f1 : <T extends { foo: unknown; 0: unknown; }>(_a: T, b: Extract<keyof T, string>) => void
>T : T
>foo : unknown
>0 : unknown
>_a : T
>T : T
>b : Extract<keyof T, string>
>Extract : Extract<T, U>
>T : T

b = "foo"; // succeeds
>b = "foo" : "foo"
>b : Extract<keyof T, string>
>"foo" : "foo"

b = 0; // errors
>b = 0 : 0
>b : Extract<keyof T, string>
>0 : 0
}

function f2<T extends { foo: unknown; 0: unknown }>(_a: T, b: Exclude<keyof T, string>) {
>f2 : <T extends { foo: unknown; 0: unknown; }>(_a: T, b: Exclude<keyof T, string>) => void
>T : T
>foo : unknown
>0 : unknown
>_a : T
>T : T
>b : Exclude<keyof T, string>
>Exclude : Exclude<T, U>
>T : T

b = "foo"; // errors
>b = "foo" : "foo"
>b : Exclude<keyof T, string>
>"foo" : "foo"

b = 0; // succeeds
>b = 0 : 0
>b : Exclude<keyof T, string>
>0 : 0
}

function f3<T extends number | string>(
>f3 : <T extends string | number>(i: T & string, j: T, b: { x: T; } extends { x: string; } ? { y: { x: T; }; } : never) => void
>T : T

i: T & string,
>i : T & string
>T : T

j: T,
>j : T
>T : T

b: { x: T } extends { x: string } ? { y: { x: T } } : never) {
>b : { x: T; } extends { x: string; } ? { y: { x: T; }; } : never
>x : T
>T : T
>x : string
>y : { x: T; }
>x : T
>T : T

b = { y: { x: i } }; // success
>b = { y: { x: i } } : { y: { x: T & string; }; }
>b : { x: T; } extends { x: string; } ? { y: { x: T; }; } : never
>{ y: { x: i } } : { y: { x: T & string; }; }
>y : { x: T & string; }
>{ x: i } : { x: T & string; }
>x : T & string
>i : T & string

b = { y: { x: j } }; // failure
>b = { y: { x: j } } : { y: { x: T; }; }
>b : { x: T; } extends { x: string; } ? { y: { x: T; }; } : never
>{ y: { x: j } } : { y: { x: T; }; }
>y : { x: T; }
>{ x: j } : { x: T; }
>x : T
>j : T
}

2 changes: 1 addition & 1 deletion tests/baselines/reference/inferTypes1.types
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ type T61<T> = infer A extends infer B ? infer C : infer D; // Error
>D : D

type T62<T> = U extends (infer U)[] ? U : U; // Error
>T62 : any
>T62 : {} | any
>T : T
>U : No type information available!
>U : U
Expand Down
12 changes: 12 additions & 0 deletions tests/baselines/reference/keyofExtractConstrainsAsExpected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//// [keyofExtractConstrainsAsExpected.ts]
type StringKeyof<T> = Extract<keyof T, string>;

type Whatever<T, K extends StringKeyof<T>> = any;

type WithoutFoo = Whatever<{ foo: string }, "foo">; // ok

// no error on the following
type WithoutFooGeneric<P extends { foo: string }> = Whatever<P, "foo">;


//// [keyofExtractConstrainsAsExpected.js]
27 changes: 27 additions & 0 deletions tests/baselines/reference/keyofExtractConstrainsAsExpected.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/keyofExtractConstrainsAsExpected.ts ===
type StringKeyof<T> = Extract<keyof T, string>;
>StringKeyof : Symbol(StringKeyof, Decl(keyofExtractConstrainsAsExpected.ts, 0, 0))
>T : Symbol(T, Decl(keyofExtractConstrainsAsExpected.ts, 0, 17))
>Extract : Symbol(Extract, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(keyofExtractConstrainsAsExpected.ts, 0, 17))

type Whatever<T, K extends StringKeyof<T>> = any;
>Whatever : Symbol(Whatever, Decl(keyofExtractConstrainsAsExpected.ts, 0, 47))
>T : Symbol(T, Decl(keyofExtractConstrainsAsExpected.ts, 2, 14))
>K : Symbol(K, Decl(keyofExtractConstrainsAsExpected.ts, 2, 16))
>StringKeyof : Symbol(StringKeyof, Decl(keyofExtractConstrainsAsExpected.ts, 0, 0))
>T : Symbol(T, Decl(keyofExtractConstrainsAsExpected.ts, 2, 14))

type WithoutFoo = Whatever<{ foo: string }, "foo">; // ok
>WithoutFoo : Symbol(WithoutFoo, Decl(keyofExtractConstrainsAsExpected.ts, 2, 49))
>Whatever : Symbol(Whatever, Decl(keyofExtractConstrainsAsExpected.ts, 0, 47))
>foo : Symbol(foo, Decl(keyofExtractConstrainsAsExpected.ts, 4, 28))

// no error on the following
type WithoutFooGeneric<P extends { foo: string }> = Whatever<P, "foo">;
>WithoutFooGeneric : Symbol(WithoutFooGeneric, Decl(keyofExtractConstrainsAsExpected.ts, 4, 51))
>P : Symbol(P, Decl(keyofExtractConstrainsAsExpected.ts, 7, 23))
>foo : Symbol(foo, Decl(keyofExtractConstrainsAsExpected.ts, 7, 34))
>Whatever : Symbol(Whatever, Decl(keyofExtractConstrainsAsExpected.ts, 0, 47))
>P : Symbol(P, Decl(keyofExtractConstrainsAsExpected.ts, 7, 23))

27 changes: 27 additions & 0 deletions tests/baselines/reference/keyofExtractConstrainsAsExpected.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/keyofExtractConstrainsAsExpected.ts ===
type StringKeyof<T> = Extract<keyof T, string>;
>StringKeyof : Extract<keyof T, string>
>T : T
>Extract : Extract<T, U>
>T : T

type Whatever<T, K extends StringKeyof<T>> = any;
>Whatever : any
>T : T
>K : K
>StringKeyof : Extract<keyof T, string>
>T : T

type WithoutFoo = Whatever<{ foo: string }, "foo">; // ok
>WithoutFoo : any
>Whatever : any
>foo : string

// no error on the following
type WithoutFooGeneric<P extends { foo: string }> = Whatever<P, "foo">;
>WithoutFooGeneric : any
>P : P
>foo : string
>Whatever : any
>P : P

18 changes: 18 additions & 0 deletions tests/cases/compiler/conditionalTypeGenericAssignability.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @strict: true
function f1<T extends { foo: unknown; 0: unknown }>(_a: T, b: Extract<keyof T, string>) {
b = "foo"; // succeeds
b = 0; // errors
}

function f2<T extends { foo: unknown; 0: unknown }>(_a: T, b: Exclude<keyof T, string>) {
b = "foo"; // errors
b = 0; // succeeds
}

function f3<T extends number | string>(
i: T & string,
j: T,
b: { x: T } extends { x: string } ? { y: { x: T } } : never) {
b = { y: { x: i } }; // success
b = { y: { x: j } }; // failure
}
8 changes: 8 additions & 0 deletions tests/cases/compiler/keyofExtractConstrainsAsExpected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type StringKeyof<T> = Extract<keyof T, string>;

type Whatever<T, K extends StringKeyof<T>> = any;

type WithoutFoo = Whatever<{ foo: string }, "foo">; // ok

// no error on the following
type WithoutFooGeneric<P extends { foo: string }> = Whatever<P, "foo">;