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
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
Add tests
  • Loading branch information
ahejlsberg committed Jan 30, 2018
commit 34505c101925fd81ee063746839de0d34b5e22f3
71 changes: 71 additions & 0 deletions tests/cases/conformance/types/conditional/inferTypes1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// @strict: true
// @declaration: true

type Unpacked<T> =
T extends (infer U)[] ? U :
T extends (...args: any[]) => infer U ? U :
T extends Promise<infer U> ? U :
T;

type T00 = Unpacked<string>; // string
type T01 = Unpacked<string[]>; // string
type T02 = Unpacked<() => string>; // string
type T03 = Unpacked<Promise<string>>; // string
type T04 = Unpacked<Unpacked<Promise<string>[]>>; // string
type T05 = Unpacked<any>; // any
type T06 = Unpacked<never>; // never

type ReturnType<T extends Function> = T extends ((...args: any[]) => infer R) | (new (...args: any[]) => infer R) ? R : any;

function f1(s: string) {
return { a: 1, b: s };
}

class C {
x = 0;
y = 0;
}

type T10 = ReturnType<() => string>; // string
type T11 = ReturnType<(s: string) => void>; // void
type T12 = ReturnType<(<T>() => T)>; // {}
type T13 = ReturnType<(<T extends U, U extends number[]>() => T)>; // number[]
type T14 = ReturnType<typeof f1>; // { a: number, b: string }
type T15 = ReturnType<typeof C>; // C
type T16 = ReturnType<any>; // any
type T17 = ReturnType<never>; // any
type T18 = ReturnType<string>; // Error
type T19 = ReturnType<Function>; // any

type ArgumentType<T extends (x: any) => any> = T extends (a: infer A) => any ? A : any;

type T20 = ArgumentType<() => void>; // never
type T21 = ArgumentType<(x: string) => number>; // string
type T22 = ArgumentType<(x?: string) => number>; // string | undefined
type T23 = ArgumentType<(...args: string[]) => number>; // string
type T24 = ArgumentType<(x: string, y: string) => number>; // Error
type T25 = ArgumentType<Function>; // Error
type T26 = ArgumentType<any>; // any
type T27 = ArgumentType<never>; // any

type X1<T extends { x: any, y: any }> = T extends { x: infer X, y: infer Y } ? [X, Y] : any;

type T30 = X1<{ x: any, y: any }>; // [any, any]
type T31 = X1<{ x: number, y: string }>; // [number, string]
type T32 = X1<{ x: number, y: string, z: boolean }>; // [number, string]

type X2<T> = T extends { a: infer U, b: infer U } ? U : never;

type T40 = X2<{}>; // never
type T41 = X2<{ a: string }>; // never
type T42 = X2<{ a: string, b: string }>; // string
type T43 = X2<{ a: number, b: string }>; // string | number
type T44 = X2<{ a: number, b: string, c: boolean }>; // string | number

type X3<T> = T extends { a: (x: infer U) => void, b: (x: infer U) => void } ? U : never;

type T50 = X3<{}>; // never
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