Skip to content
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

Type inference in conditional types #21496

Merged
merged 66 commits into from
Feb 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
57ca768
Initial implementation of conditional type operator
ahejlsberg Dec 5, 2017
063eed1
Add type relationships and distribute over union types
ahejlsberg Dec 8, 2017
ec2bdfd
Add 'T extends U' type operator
ahejlsberg Dec 12, 2017
43e195d
Clean up isGenericXXXType functions
ahejlsberg Dec 12, 2017
61225cc
Introduce TypeFlags.Instatiable
ahejlsberg Dec 13, 2017
9f74a7a
Rename TypeVariable to InstantiableType
ahejlsberg Dec 13, 2017
20434fa
Inference for conditional and extends type operators
ahejlsberg Dec 13, 2017
ddc631c
Fix typo
ahejlsberg Dec 13, 2017
000f121
Improve conditional type constraint checking
ahejlsberg Dec 13, 2017
27b945b
Handle constraints for distributive conditional types
ahejlsberg Dec 16, 2017
f59e2e6
Accept new baselines
ahejlsberg Dec 17, 2017
14590f1
Move JsxAttributes and MarkerType from TypeFlags to ObjectFlags
ahejlsberg Dec 18, 2017
100e4f6
Accept new baselines
ahejlsberg Dec 18, 2017
c5fd2f1
Parse xxx? as JSDoc type when not followed by token that starts type
ahejlsberg Dec 19, 2017
341c397
Accept new baselines
ahejlsberg Dec 19, 2017
3f4911f
Fix linting error
ahejlsberg Dec 20, 2017
abc8110
Merge branch 'master' into conditionalTypes
ahejlsberg Jan 3, 2018
bb23bb2
Propagate both TypeFlags and ObjectFlags in getSpreadType
ahejlsberg Jan 3, 2018
c10a552
Eagerly evaluate S extends T when S is definitely or definitely not a…
ahejlsberg Jan 14, 2018
53b1572
Revert to extends check being part of conditional type
ahejlsberg Jan 15, 2018
5094f76
Remove 'T extends U' type constructor
ahejlsberg Jan 15, 2018
925da86
Accept new baselines
ahejlsberg Jan 15, 2018
e8d1740
Introduce substitution types to use for constrained type parameters
ahejlsberg Jan 15, 2018
15baf0e
Accept new baselines
ahejlsberg Jan 15, 2018
9598acd
Properly handle 'any' and 'never' as conditional check type
ahejlsberg Jan 15, 2018
e96ec8c
Erase substitution types in type references and type alias instantiat…
ahejlsberg Jan 16, 2018
d52fa71
Optimize the sameMap function
ahejlsberg Jan 16, 2018
4ec6fdd
Merge branch 'master' into conditionalTypes
ahejlsberg Jan 17, 2018
fd0dd6e
Separate code path for conditional type instantiation
ahejlsberg Jan 18, 2018
c360c24
Fix parsing
ahejlsberg Jan 19, 2018
0e73240
Disallow conditional type following 'extends'
ahejlsberg Jan 19, 2018
5204fd5
Add T is related to { [P in xxx]: T[P] } type relationship
ahejlsberg Jan 20, 2018
eb314d0
Add tests
ahejlsberg Jan 20, 2018
cdd50d4
Accept new baselines
ahejlsberg Jan 20, 2018
fc7d1c3
Revise comments
ahejlsberg Jan 20, 2018
f19959a
Cache substitution types and remove erasure that was too eager
ahejlsberg Jan 20, 2018
b869290
Remove unnecessary caching of substitution types
ahejlsberg Jan 21, 2018
4c7ec3c
Shared code path for getConditionalType and instantiateConditionalType
ahejlsberg Jan 21, 2018
b42c6b1
Only conditional types that check naked type parameter distribute ove…
ahejlsberg Jan 24, 2018
8e337b5
Fix bug in resolveMappedTypeMembers
ahejlsberg Jan 24, 2018
d608941
Implement type inference in conditional types
ahejlsberg Jan 25, 2018
490d656
Remove pointless fourslash tests
ahejlsberg Jan 25, 2018
4d62df4
Accept new baselines
ahejlsberg Jan 25, 2018
ef0c3b7
Change precedence of 'infer T' to match 'keyof T'
ahejlsberg Jan 25, 2018
f59f3a2
Check 'infer' declarations are in extends clause of conditional type
ahejlsberg Jan 25, 2018
60c9249
Properly handle inferred type parameters in declaration emitter
ahejlsberg Jan 26, 2018
e450212
Infer intersection types for multiple contravariant candidates
ahejlsberg Jan 26, 2018
fa6c4b7
Check for definitely false condition first
ahejlsberg Jan 26, 2018
edffb12
Cache 'any' instantiations for faster conditional type resolution
ahejlsberg Jan 27, 2018
c6f0dfb
Add definitelyAssignableRelation
ahejlsberg Jan 30, 2018
4f2b5f3
Merge branch 'master' into conditionalTypes
ahejlsberg Jan 30, 2018
f990e4e
Merge branch 'master' into conditionalTypes
ahejlsberg Jan 30, 2018
01516c8
Update to use TypeFlags.Instantiable in instantiateSymbol
ahejlsberg Jan 30, 2018
34505c1
Add tests
ahejlsberg Jan 30, 2018
03a3847
Accept new baselines
ahejlsberg Jan 30, 2018
7c241ba
Merge branch 'conditionalTypes' into inferTypes
ahejlsberg Jan 30, 2018
5702f61
Always use strict rules for contravariant inferences
ahejlsberg Jan 31, 2018
1275a20
Accept new baselines
ahejlsberg Jan 31, 2018
afec056
Add more error case checks
ahejlsberg Jan 31, 2018
36a49ad
Accept new baselines
ahejlsberg Jan 31, 2018
d4dc67a
Merge branch 'master' into conditionalTypes
ahejlsberg Feb 3, 2018
7034d67
Merge branch 'conditionalTypes' into inferTypes
ahejlsberg Feb 3, 2018
7bb8cfe
Resolve to type in true branch for 'never extends never'
ahejlsberg Feb 3, 2018
a712344
Add tests
ahejlsberg Feb 3, 2018
04260d6
Accept new baselines
ahejlsberg Feb 3, 2018
4ae8445
Properly classify output text in emitter
ahejlsberg Feb 3, 2018
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
Prev Previous commit
Next Next commit
Accept new baselines
  • Loading branch information
ahejlsberg committed Jan 31, 2018
commit 36a49adac4e434f26c9cecc7ac5d99b1aca2538c
30 changes: 29 additions & 1 deletion tests/baselines/reference/inferTypes1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(34,23): error TS2344: T
tests/cases/conformance/types/conditional/inferTypes1.ts(43,25): error TS2344: Type '(x: string, y: string) => number' does not satisfy the constraint '(x: any) => any'.
tests/cases/conformance/types/conditional/inferTypes1.ts(44,25): error TS2344: Type 'Function' does not satisfy the constraint '(x: any) => any'.
Type 'Function' provides no match for the signature '(x: any): any'.
tests/cases/conformance/types/conditional/inferTypes1.ts(70,12): error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
tests/cases/conformance/types/conditional/inferTypes1.ts(71,15): error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
tests/cases/conformance/types/conditional/inferTypes1.ts(71,41): error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
tests/cases/conformance/types/conditional/inferTypes1.ts(71,51): error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
tests/cases/conformance/types/conditional/inferTypes1.ts(72,15): error TS2304: Cannot find name 'U'.
tests/cases/conformance/types/conditional/inferTypes1.ts(72,15): error TS4081: Exported type alias 'T62' has or is using private name 'U'.
tests/cases/conformance/types/conditional/inferTypes1.ts(72,43): error TS2304: Cannot find name 'U'.
tests/cases/conformance/types/conditional/inferTypes1.ts(72,43): error TS4081: Exported type alias 'T62' has or is using private name 'U'.


==== tests/cases/conformance/types/conditional/inferTypes1.ts (3 errors) ====
==== tests/cases/conformance/types/conditional/inferTypes1.ts (11 errors) ====
type Unpacked<T> =
T extends (infer U)[] ? U :
T extends (...args: any[]) => infer U ? U :
Expand Down Expand Up @@ -80,4 +88,24 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(44,25): error TS2344: T
type T52 = X3<{ a: (x: string) => void, b: (x: string) => void }>; // string
type T53 = X3<{ a: (x: number) => void, b: (x: string) => void }>; // string & number
type T54 = X3<{ a: (x: number) => void, b: () => void }>; // number

type T60 = infer U; // Error
~~~~~~~
!!! error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
type T61<T> = infer A extends infer B ? infer C : infer D; // Error
~~~~~~~
!!! error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
~~~~~~~
!!! error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
~~~~~~~
!!! error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
type T62<T> = U extends (infer U)[] ? U : U; // Error
~
!!! error TS2304: Cannot find name 'U'.
~
!!! error TS4081: Exported type alias 'T62' has or is using private name 'U'.
~
!!! error TS2304: Cannot find name 'U'.
~
!!! error TS4081: Exported type alias 'T62' has or is using private name 'U'.

104 changes: 4 additions & 100 deletions tests/baselines/reference/inferTypes1.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ type T51 = X3<{ a: (x: string) => void }>; // never
type T52 = X3<{ a: (x: string) => void, b: (x: string) => void }>; // string
type T53 = X3<{ a: (x: number) => void, b: (x: string) => void }>; // string & number
type T54 = X3<{ a: (x: number) => void, b: () => void }>; // number

type T60 = infer U; // Error
type T61<T> = infer A extends infer B ? infer C : infer D; // Error
type T62<T> = U extends (infer U)[] ? U : U; // Error


//// [inferTypes1.js]
Expand All @@ -81,103 +85,3 @@ var C = /** @class */ (function () {
}
return C;
}());


//// [inferTypes1.d.ts]
declare type Unpacked<T> = T extends (infer U)[] ? U : T extends (...args: any[]) => infer U ? U : T extends Promise<infer U> ? U : T;
declare type T00 = Unpacked<string>;
declare type T01 = Unpacked<string[]>;
declare type T02 = Unpacked<() => string>;
declare type T03 = Unpacked<Promise<string>>;
declare type T04 = Unpacked<Unpacked<Promise<string>[]>>;
declare type T05 = Unpacked<any>;
declare type T06 = Unpacked<never>;
declare type ReturnType<T extends Function> = T extends ((...args: any[]) => infer R) | (new (...args: any[]) => infer R) ? R : any;
declare function f1(s: string): {
a: number;
b: string;
};
declare class C {
x: number;
y: number;
}
declare type T10 = ReturnType<() => string>;
declare type T11 = ReturnType<(s: string) => void>;
declare type T12 = ReturnType<(<T>() => T)>;
declare type T13 = ReturnType<(<T extends U, U extends number[]>() => T)>;
declare type T14 = ReturnType<typeof f1>;
declare type T15 = ReturnType<typeof C>;
declare type T16 = ReturnType<any>;
declare type T17 = ReturnType<never>;
declare type T18 = ReturnType<string>;
declare type T19 = ReturnType<Function>;
declare type ArgumentType<T extends (x: any) => any> = T extends (a: infer A) => any ? A : any;
declare type T20 = ArgumentType<() => void>;
declare type T21 = ArgumentType<(x: string) => number>;
declare type T22 = ArgumentType<(x?: string) => number>;
declare type T23 = ArgumentType<(...args: string[]) => number>;
declare type T24 = ArgumentType<(x: string, y: string) => number>;
declare type T25 = ArgumentType<Function>;
declare type T26 = ArgumentType<any>;
declare type T27 = ArgumentType<never>;
declare type X1<T extends {
x: any;
y: any;
}> = T extends {
x: infer X;
y: infer Y;
} ? [X, Y] : any;
declare type T30 = X1<{
x: any;
y: any;
}>;
declare type T31 = X1<{
x: number;
y: string;
}>;
declare type T32 = X1<{
x: number;
y: string;
z: boolean;
}>;
declare type X2<T> = T extends {
a: infer U;
b: infer U;
} ? U : never;
declare type T40 = X2<{}>;
declare type T41 = X2<{
a: string;
}>;
declare type T42 = X2<{
a: string;
b: string;
}>;
declare type T43 = X2<{
a: number;
b: string;
}>;
declare type T44 = X2<{
a: number;
b: string;
c: boolean;
}>;
declare type X3<T> = T extends {
a: (x: infer U) => void;
b: (x: infer U) => void;
} ? U : never;
declare type T50 = X3<{}>;
declare type T51 = X3<{
a: (x: string) => void;
}>;
declare type T52 = X3<{
a: (x: string) => void;
b: (x: string) => void;
}>;
declare type T53 = X3<{
a: (x: number) => void;
b: (x: string) => void;
}>;
declare type T54 = X3<{
a: (x: number) => void;
b: () => void;
}>;
18 changes: 18 additions & 0 deletions tests/baselines/reference/inferTypes1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,21 @@ type T54 = X3<{ a: (x: number) => void, b: () => void }>; // number
>x : Symbol(x, Decl(inferTypes1.ts, 67, 20))
>b : Symbol(b, Decl(inferTypes1.ts, 67, 39))

type T60 = infer U; // Error
>T60 : Symbol(T60, Decl(inferTypes1.ts, 67, 57))
>U : Symbol(U, Decl(inferTypes1.ts, 69, 16))

type T61<T> = infer A extends infer B ? infer C : infer D; // Error
>T61 : Symbol(T61, Decl(inferTypes1.ts, 69, 19))
>T : Symbol(T, Decl(inferTypes1.ts, 70, 9))
>A : Symbol(A, Decl(inferTypes1.ts, 70, 19))
>B : Symbol(B, Decl(inferTypes1.ts, 70, 35))
>C : Symbol(C, Decl(inferTypes1.ts, 70, 45))
>D : Symbol(D, Decl(inferTypes1.ts, 70, 55))

type T62<T> = U extends (infer U)[] ? U : U; // Error
>T62 : Symbol(T62, Decl(inferTypes1.ts, 70, 58))
>T : Symbol(T, Decl(inferTypes1.ts, 71, 9))
>U : Symbol(U, Decl(inferTypes1.ts, 71, 30))
>U : Symbol(U, Decl(inferTypes1.ts, 71, 30))

20 changes: 20 additions & 0 deletions tests/baselines/reference/inferTypes1.types
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,23 @@ type T54 = X3<{ a: (x: number) => void, b: () => void }>; // number
>x : number
>b : () => void

type T60 = infer U; // Error
>T60 : U
>U : U

type T61<T> = infer A extends infer B ? infer C : infer D; // Error
>T61 : never
>T : T
>A : A
>B : B
>C : C
>D : D

type T62<T> = U extends (infer U)[] ? U : U; // Error
>T62 : any
>T : T
>U : No type information available!
>U : U
>U : U
>U : No type information available!