-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Higher order function type inference #30215
Merged
Merged
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b44bfaa
Convert CheckMode to a flags style enum
ahejlsberg 23473e0
Skip generic functions along with context sensitive arguments
ahejlsberg db5ca33
Merge branch 'master' into deferGenericFunctionInference
ahejlsberg 35ebbec
Minor fixes
ahejlsberg 91f8fc6
Defer calls to generic functions returning generic functions
ahejlsberg 304e25c
Add tests
ahejlsberg e7881a4
Accept new baselines
ahejlsberg c344ef3
Infer higher order function types when possible
ahejlsberg c58819e
Accept new baselines
ahejlsberg 919ade1
Improve scheme for creating unique type parameter names
ahejlsberg 549c684
Minor fix
ahejlsberg 22c934a
Add tests
ahejlsberg 5fe8ebb
Accept new baselines
ahejlsberg bf326aa
Fix lint error
ahejlsberg ad823da
Consistently defer generic functions to second type inference pass
ahejlsberg cde9444
Accept new baselines
ahejlsberg 6c790c0
Remove unnecessary excludeArgument array and getExcludeArgument function
ahejlsberg 6d88251
Minor change to heuristic for deferring generic calls
ahejlsberg b34fe67
Fix issue of more inferences leading to worse results
ahejlsberg a9e924b
Fix check for function type (allow checked type to have overloads)
ahejlsberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,8 @@ var e = <K>(x: string, y?: K) => x.length; | |
>length : number | ||
|
||
var r99 = map(e); // should be {}[] for S since a generic lambda is not inferentially typed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this comment (and the one below on GH) need to be updated, |
||
>r99 : (a: {}[]) => number[] | ||
>map(e) : (a: {}[]) => number[] | ||
>r99 : <K>(a: string[]) => number[] | ||
>map(e) : <K>(a: string[]) => number[] | ||
>map : <S, T>(f: (x: S) => T) => (a: S[]) => T[] | ||
>e : <K>(x: string, y?: K) => number | ||
|
||
|
@@ -37,8 +37,8 @@ var e2 = <K>(x: string, y?: K) => x.length; | |
>length : number | ||
|
||
var r100 = map2(e2); // type arg inference should fail for S since a generic lambda is not inferentially typed. Falls back to { length: number } | ||
>r100 : (a: { length: number; }[]) => number[] | ||
>map2(e2) : (a: { length: number; }[]) => number[] | ||
>r100 : <K>(a: string[]) => number[] | ||
>map2(e2) : <K>(a: string[]) => number[] | ||
>map2 : <S extends { length: number; }, T>(f: (x: S) => T) => (a: S[]) => T[] | ||
>e2 : <K>(x: string, y?: K) => number | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
tests/baselines/reference/genericFunctionInference1.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
tests/cases/compiler/genericFunctionInference1.ts(83,14): error TS2345: Argument of type '<a>(value: { key: a; }) => a' is not assignable to parameter of type '(value: Data) => string'. | ||
Type 'number' is not assignable to type 'string'. | ||
|
||
|
||
==== tests/cases/compiler/genericFunctionInference1.ts (1 errors) ==== | ||
declare function pipe<A extends any[], B>(ab: (...args: A) => B): (...args: A) => B; | ||
declare function pipe<A extends any[], B, C>(ab: (...args: A) => B, bc: (b: B) => C): (...args: A) => C; | ||
declare function pipe<A extends any[], B, C, D>(ab: (...args: A) => B, bc: (b: B) => C, cd: (c: C) => D): (...args: A) => D; | ||
|
||
declare function list<T>(a: T): T[]; | ||
declare function box<V>(x: V): { value: V }; | ||
declare function foo<T extends { value: T }>(x: T): T; | ||
|
||
const f00 = pipe(list); | ||
const f01 = pipe(list, box); | ||
const f02 = pipe(box, list); | ||
const f03 = pipe(x => list(x), box); | ||
const f04 = pipe(list, x => box(x)); | ||
const f05 = pipe(x => list(x), x => box(x)) | ||
const f06 = pipe(list, pipe(box)); | ||
const f07 = pipe(x => list(x), pipe(box)); | ||
const f08 = pipe(x => list(x), pipe(x => box(x))); | ||
const f09 = pipe(list, x => x.length); | ||
const f10 = pipe(foo); | ||
const f11 = pipe(foo, foo); | ||
|
||
const g00: <T>(x: T) => T[] = pipe(list); | ||
const g01: <T>(x: T) => { value: T[] } = pipe(list, box); | ||
const g02: <T>(x: T) => { value: T }[] = pipe(box, list); | ||
const g03: <T>(x: T) => { value: T[] } = pipe(x => list(x), box); | ||
const g04: <T>(x: T) => { value: T[] } = pipe(list, x => box(x)); | ||
const g05: <T>(x: T) => { value: T[] } = pipe(x => list(x), x => box(x)) | ||
const g06: <T>(x: T) => { value: T[] } = pipe(list, pipe(box)); | ||
const g07: <T>(x: T) => { value: T[] } = pipe(x => list(x), pipe(box)); | ||
const g08: <T>(x: T) => { value: T[] } = pipe(x => list(x), pipe(x => box(x))); | ||
const g09: <T>(x: T) => number = pipe(list, x => x.length); | ||
const g10: <T extends { value: T }>(x: T) => T = pipe(foo); | ||
const g12: <T extends { value: T }>(x: T) => T = pipe(foo, foo); | ||
|
||
declare function pipe2<A, B, C, D>(ab: (a: A) => B, cd: (c: C) => D): (a: [A, C]) => [B, D]; | ||
|
||
const f20 = pipe2(list, box); | ||
const f21 = pipe2(box, list); | ||
const f22 = pipe2(list, list); | ||
const f23 = pipe2(box, box); | ||
const f24 = pipe2(f20, f20); | ||
const f25 = pipe2(foo, foo); | ||
const f26 = pipe2(f25, f25); | ||
|
||
declare function pipe3<A, B, C>(ab: (a: A) => B, ac: (a: A) => C): (a: A) => [B, C]; | ||
|
||
const f30 = pipe3(list, box); | ||
const f31 = pipe3(box, list); | ||
const f32 = pipe3(list, list); | ||
|
||
declare function pipe4<A, B, C>(funcs: [(a: A) => B, (b: B) => C]): (a: A) => C; | ||
|
||
const f40 = pipe4([list, box]); | ||
const f41 = pipe4([box, list]); | ||
|
||
declare function pipe5<A, B>(f: (a: A) => B): { f: (a: A) => B }; | ||
|
||
const f50 = pipe5(list); // No higher order inference | ||
|
||
// #417 | ||
|
||
function mirror<A, B>(f: (a: A) => B): (a: A) => B { return f; } | ||
var identityM = mirror(identity); | ||
|
||
var x = 1; | ||
var y = identity(x); | ||
var z = identityM(x); | ||
|
||
// #3038 | ||
|
||
export function keyOf<a>(value: { key: a; }): a { | ||
return value.key; | ||
} | ||
export interface Data { | ||
key: number; | ||
value: Date; | ||
} | ||
|
||
var data: Data[] = []; | ||
|
||
declare function toKeys<a>(values: a[], toKey: (value: a) => string): string[]; | ||
|
||
toKeys(data, keyOf); // Error | ||
~~~~~ | ||
!!! error TS2345: Argument of type '<a>(value: { key: a; }) => a' is not assignable to parameter of type '(value: Data) => string'. | ||
!!! error TS2345: Type 'number' is not assignable to type 'string'. | ||
|
||
// #9366 | ||
|
||
function flip<a, b, c>(f: (a: a, b: b) => c): (b: b, a: a) => c { | ||
return (b: b, a: a) => f(a, b); | ||
} | ||
function zip<T, U>(x: T, y: U): [T, U] { | ||
return [x, y]; | ||
} | ||
|
||
var expected: <T, U>(y: U, x: T) => [T, U] = flip(zip); | ||
var actual = flip(zip); | ||
|
||
// #9366 | ||
|
||
const map = <T, U>(transform: (t: T) => U) => | ||
(arr: T[]) => arr.map(transform) | ||
|
||
const identityStr = (t: string) => t; | ||
|
||
const arr: string[] = map(identityStr)(['a']); | ||
const arr1: string[] = map(identity)(['a']); | ||
|
||
// #9949 | ||
|
||
function of2<a, b>(one: a, two: b): [a, b] { | ||
return [one, two]; | ||
} | ||
|
||
const flipped = flip(of2); | ||
|
||
// #29904.1 | ||
|
||
type Component<P> = (props: P) => {}; | ||
|
||
declare const myHoc1: <P>(C: Component<P>) => Component<P>; | ||
declare const myHoc2: <P>(C: Component<P>) => Component<P>; | ||
|
||
declare const MyComponent1: Component<{ foo: 1 }>; | ||
|
||
const enhance = pipe( | ||
myHoc1, | ||
myHoc2, | ||
); | ||
|
||
const MyComponent2 = enhance(MyComponent1); | ||
|
||
// #29904.2 | ||
|
||
const fn20 = pipe((_a?: {}) => 1); | ||
|
||
// #29904.3 | ||
|
||
type Fn = (n: number) => number; | ||
const fn30: Fn = pipe( | ||
x => x + 1, | ||
x => x * 2, | ||
); | ||
|
||
const promise = Promise.resolve(1); | ||
promise.then( | ||
pipe( | ||
x => x + 1, | ||
x => x * 2, | ||
), | ||
); | ||
|
||
// #29904.4 | ||
|
||
declare const getString: () => string; | ||
declare const orUndefined: (name: string) => string | undefined; | ||
declare const identity: <T>(value: T) => T; | ||
|
||
const fn40 = pipe( | ||
getString, | ||
string => orUndefined(string), | ||
identity, | ||
); | ||
|
||
// #29904.6 | ||
|
||
declare const getArray: () => string[]; | ||
declare const first: <T>(ts: T[]) => T; | ||
|
||
const fn60 = pipe( | ||
getArray, | ||
x => x, | ||
first, | ||
); | ||
|
||
const fn61 = pipe( | ||
getArray, | ||
identity, | ||
first, | ||
); | ||
|
||
const fn62 = pipe( | ||
getArray, | ||
x => x, | ||
x => first(x), | ||
); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this type parameter addition required? Seems strange to now need it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's what's happening: Previously we'd infer
number
fromerrorCodes
, then fix that inference as we contextually typedequateValues
, and finally ignore inferences fromcompareValues
because of fixing. Now we defer processing ofequateValues
to get the best possible contextual type, which means we'll infer from botherrorCodes
andcompareValues
. And fromcompareValues
we get the candidatenumber | undefined
which is technically contravariant, but we ignore that because we're not in--strictFunctionTypes
mode. So, we end up with a worse inference. The real fix here would be for us to do the work to use--strictFunctionTypes
in the compiler, the legacy rules just aren't right but it is a breaking change (in other places) to fix them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably too far gone to change; but is there a good compat reason we can't perform inferences the same way outside and inside
strictFunctionTypes
mode?But aight, if that's the case I guess it can't be avoided. :(