Skip to content

Check inferred constraints for 'infer X' type variables #22323

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 4 commits into from
Mar 5, 2018
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
143 changes: 73 additions & 70 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3966,7 +3966,8 @@ namespace ts {

/* @internal */
export interface InferenceContext extends TypeMapper {
signature: Signature; // Generic signature for which inferences are made
typeParameters: TypeParameter[]; // Type parameters for which inferences are made
signature: Signature; // Generic signature for which inferences are made (if any)
inferences: InferenceInfo[]; // Inferences made for each type parameter
flags: InferenceFlags; // Inference flags
compareTypes: TypeComparer; // Type comparer function
Expand Down
19 changes: 18 additions & 1 deletion tests/baselines/reference/inferTypes1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(75,43): error TS2304: C
tests/cases/conformance/types/conditional/inferTypes1.ts(75,43): error TS4081: Exported type alias 'T62' has or is using private name 'U'.
tests/cases/conformance/types/conditional/inferTypes1.ts(81,44): error TS2344: Type 'U' does not satisfy the constraint 'string'.
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/types/conditional/inferTypes1.ts(134,40): error TS2322: Type 'T' is not assignable to type 'string'.
tests/cases/conformance/types/conditional/inferTypes1.ts(143,40): error TS2322: Type 'T' is not assignable to type 'string'.


==== tests/cases/conformance/types/conditional/inferTypes1.ts (16 errors) ====
Expand Down Expand Up @@ -144,6 +144,15 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(134,40): error TS2322:
type T77<T> = T extends T76<infer X, infer Y> ? T76<X, Y> : never;
type T78<T> = T extends T76<infer X, infer X> ? T76<X, X> : never;

type Foo<T extends string, U extends T> = [T, U];
type Bar<T> = T extends Foo<infer X, infer Y> ? Foo<X, Y> : never;

type T90 = Bar<[string, string]>; // [string, string]
type T91 = Bar<[string, "a"]>; // [string, "a"]
type T92 = Bar<[string, "a"] & { x: string }>; // [string, "a"]
type T93 = Bar<["a", string]>; // never
type T94 = Bar<[number, number]>; // never

// Example from #21496

type JsonifiedObject<T extends object> = { [K in keyof T]: Jsonified<T[K]> };
Expand Down Expand Up @@ -206,4 +215,12 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(134,40): error TS2322:

type T80 = MatchingKeys<test, void>;
type T81 = VoidKeys<test>;

// Repro from #22221

type MustBeString<T extends string> = T;
type EnsureIsString<T> = T extends MustBeString<infer U> ? U : never;

type Test1 = EnsureIsString<"hello">; // "hello"
type Test2 = EnsureIsString<42>; // never

17 changes: 17 additions & 0 deletions tests/baselines/reference/inferTypes1.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ type T76<T extends T[], U extends T> = { x: T };
type T77<T> = T extends T76<infer X, infer Y> ? T76<X, Y> : never;
type T78<T> = T extends T76<infer X, infer X> ? T76<X, X> : never;

type Foo<T extends string, U extends T> = [T, U];
type Bar<T> = T extends Foo<infer X, infer Y> ? Foo<X, Y> : never;

type T90 = Bar<[string, string]>; // [string, string]
type T91 = Bar<[string, "a"]>; // [string, "a"]
type T92 = Bar<[string, "a"] & { x: string }>; // [string, "a"]
type T93 = Bar<["a", string]>; // never
type T94 = Bar<[number, number]>; // never

// Example from #21496

type JsonifiedObject<T extends object> = { [K in keyof T]: Jsonified<T[K]> };
Expand Down Expand Up @@ -148,6 +157,14 @@ interface test {

type T80 = MatchingKeys<test, void>;
type T81 = VoidKeys<test>;

// Repro from #22221

type MustBeString<T extends string> = T;
type EnsureIsString<T> = T extends MustBeString<infer U> ? U : never;

type Test1 = EnsureIsString<"hello">; // "hello"
type Test2 = EnsureIsString<42>; // never


//// [inferTypes1.js]
Expand Down
Loading