-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Lazily compute signature type predicates #17600
Merged
Merged
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
bf6a3f7
Lazily compute signature type predicates
b2a29de
Use an instance of IdentifierTypePredicate to represent an unresolved…
a072a64
Merge branch 'master' into typepredicate
2d0dd82
Simplify `getMaybeTypePredicate`
304665a
Invert representation of `resolvedTypePredicate`
7350faa
Remove `__unresolvedTypePredicate` type and remember to use `noTypePr…
2bc75c0
Merge branch 'master' into typepredicate
9fe2a59
Merge branch 'master' into typepredicate
cadf250
Fix style of getTypePredicateOfSignature
7001e71
Use in createGetSymbolWalker
d52812f
Fix bugs for unions of type predicates
b384c9c
Merge branch 'master' into typepredicate
e72f847
Code review
aaa6928
Make noTypePredicate purely an implementation detail of getTypePredic…
ahejlsberg 3e1b337
Merge branch 'master' into typepredicate
8b6fbd7
Add test
267c3a9
Merge branch 'master' into typepredicate
8e0b1e9
Add test for #19642
7c5718c
Merge branch 'master' into typepredicate
4a8b449
Add test with reversed order
85d70e4
Merge branch 'master' into typepredicate
8d2ea56
Merge branch 'master' into typepredicate
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
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//// [typeInferenceTypePredicate.ts] | ||
declare function f<T>(predicate: (x: {}) => x is T): T; | ||
// 'res' should be of type 'number'. | ||
const res = f((n): n is number => true); | ||
|
||
|
||
//// [typeInferenceTypePredicate.js] | ||
// 'res' should be of type 'number'. | ||
var res = f(function (n) { return true; }); |
17 changes: 17 additions & 0 deletions
17
tests/baselines/reference/typeInferenceTypePredicate.symbols
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,17 @@ | ||
=== tests/cases/compiler/typeInferenceTypePredicate.ts === | ||
declare function f<T>(predicate: (x: {}) => x is T): T; | ||
>f : Symbol(f, Decl(typeInferenceTypePredicate.ts, 0, 0)) | ||
>T : Symbol(T, Decl(typeInferenceTypePredicate.ts, 0, 19)) | ||
>predicate : Symbol(predicate, Decl(typeInferenceTypePredicate.ts, 0, 22)) | ||
>x : Symbol(x, Decl(typeInferenceTypePredicate.ts, 0, 34)) | ||
>x : Symbol(x, Decl(typeInferenceTypePredicate.ts, 0, 34)) | ||
>T : Symbol(T, Decl(typeInferenceTypePredicate.ts, 0, 19)) | ||
>T : Symbol(T, Decl(typeInferenceTypePredicate.ts, 0, 19)) | ||
|
||
// 'res' should be of type 'number'. | ||
const res = f((n): n is number => true); | ||
>res : Symbol(res, Decl(typeInferenceTypePredicate.ts, 2, 5)) | ||
>f : Symbol(f, Decl(typeInferenceTypePredicate.ts, 0, 0)) | ||
>n : Symbol(n, Decl(typeInferenceTypePredicate.ts, 2, 15)) | ||
>n : Symbol(n, Decl(typeInferenceTypePredicate.ts, 2, 15)) | ||
|
20 changes: 20 additions & 0 deletions
20
tests/baselines/reference/typeInferenceTypePredicate.types
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,20 @@ | ||
=== tests/cases/compiler/typeInferenceTypePredicate.ts === | ||
declare function f<T>(predicate: (x: {}) => x is T): T; | ||
>f : <T>(predicate: (x: {}) => x is T) => T | ||
>T : T | ||
>predicate : (x: {}) => x is T | ||
>x : {} | ||
>x : any | ||
>T : T | ||
>T : T | ||
|
||
// 'res' should be of type 'number'. | ||
const res = f((n): n is number => true); | ||
>res : number | ||
>f((n): n is number => true) : number | ||
>f : <T>(predicate: (x: {}) => x is T) => T | ||
>(n): n is number => true : (n: {}) => n is number | ||
>n : {} | ||
>n : any | ||
>true : true | ||
|
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,10 @@ | ||
//// [typeInferenceTypePredicate2.ts] | ||
[true, true, false, null] | ||
.filter((thing): thing is boolean => thing !== null) | ||
.map(thing => thing.toString()); | ||
|
||
|
||
//// [typeInferenceTypePredicate2.js] | ||
[true, true, false, null] | ||
.filter(function (thing) { return thing !== null; }) | ||
.map(function (thing) { return thing.toString(); }); |
18 changes: 18 additions & 0 deletions
18
tests/baselines/reference/typeInferenceTypePredicate2.symbols
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,18 @@ | ||
=== tests/cases/compiler/typeInferenceTypePredicate2.ts === | ||
[true, true, false, null] | ||
>[true, true, false, null] .filter((thing): thing is boolean => thing !== null) .map : Symbol(Array.map, Decl(lib.d.ts, --, --)) | ||
>[true, true, false, null] .filter : Symbol(Array.filter, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) | ||
|
||
.filter((thing): thing is boolean => thing !== null) | ||
>filter : Symbol(Array.filter, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) | ||
>thing : Symbol(thing, Decl(typeInferenceTypePredicate2.ts, 1, 13)) | ||
>thing : Symbol(thing, Decl(typeInferenceTypePredicate2.ts, 1, 13)) | ||
>thing : Symbol(thing, Decl(typeInferenceTypePredicate2.ts, 1, 13)) | ||
|
||
.map(thing => thing.toString()); | ||
>map : Symbol(Array.map, Decl(lib.d.ts, --, --)) | ||
>thing : Symbol(thing, Decl(typeInferenceTypePredicate2.ts, 2, 9)) | ||
>thing.toString : Symbol(Object.toString, Decl(lib.d.ts, --, --)) | ||
>thing : Symbol(thing, Decl(typeInferenceTypePredicate2.ts, 2, 9)) | ||
>toString : Symbol(Object.toString, Decl(lib.d.ts, --, --)) | ||
|
30 changes: 30 additions & 0 deletions
30
tests/baselines/reference/typeInferenceTypePredicate2.types
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,30 @@ | ||
=== tests/cases/compiler/typeInferenceTypePredicate2.ts === | ||
[true, true, false, null] | ||
>[true, true, false, null] .filter((thing): thing is boolean => thing !== null) .map(thing => thing.toString()) : string[] | ||
>[true, true, false, null] .filter((thing): thing is boolean => thing !== null) .map : <U>(callbackfn: (value: boolean, index: number, array: boolean[]) => U, thisArg?: any) => U[] | ||
>[true, true, false, null] .filter((thing): thing is boolean => thing !== null) : boolean[] | ||
>[true, true, false, null] .filter : { <S extends boolean>(callbackfn: (value: boolean, index: number, array: boolean[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: boolean, index: number, array: boolean[]) => any, thisArg?: any): boolean[]; } | ||
>[true, true, false, null] : boolean[] | ||
>true : true | ||
>true : true | ||
>false : false | ||
>null : null | ||
|
||
.filter((thing): thing is boolean => thing !== null) | ||
>filter : { <S extends boolean>(callbackfn: (value: boolean, index: number, array: boolean[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: boolean, index: number, array: boolean[]) => any, thisArg?: any): boolean[]; } | ||
>(thing): thing is boolean => thing !== null : (thing: boolean) => thing is boolean | ||
>thing : boolean | ||
>thing : any | ||
>thing !== null : boolean | ||
>thing : boolean | ||
>null : null | ||
|
||
.map(thing => thing.toString()); | ||
>map : <U>(callbackfn: (value: boolean, index: number, array: boolean[]) => U, thisArg?: any) => U[] | ||
>thing => thing.toString() : (thing: boolean) => string | ||
>thing : boolean | ||
>thing.toString() : string | ||
>thing.toString : () => string | ||
>thing : boolean | ||
>toString : () => string | ||
|
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,23 @@ | ||
//// [typePredicatesInUnion.ts] | ||
interface A { | ||
pred(x: {}): x is boolean; | ||
} | ||
interface B { | ||
pred(x: {}): x is string; | ||
} | ||
|
||
type Or = A | B; | ||
|
||
function f(o: Or, x: {}) { | ||
if (o.pred(x)) { | ||
x; | ||
} | ||
} | ||
|
||
|
||
//// [typePredicatesInUnion.js] | ||
function f(o, x) { | ||
if (o.pred(x)) { | ||
x; | ||
} | ||
} |
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,40 @@ | ||
=== tests/cases/compiler/typePredicatesInUnion.ts === | ||
interface A { | ||
>A : Symbol(A, Decl(typePredicatesInUnion.ts, 0, 0)) | ||
|
||
pred(x: {}): x is boolean; | ||
>pred : Symbol(A.pred, Decl(typePredicatesInUnion.ts, 0, 13)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 1, 9)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 1, 9)) | ||
} | ||
interface B { | ||
>B : Symbol(B, Decl(typePredicatesInUnion.ts, 2, 1)) | ||
|
||
pred(x: {}): x is string; | ||
>pred : Symbol(B.pred, Decl(typePredicatesInUnion.ts, 3, 13)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 4, 9)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 4, 9)) | ||
} | ||
|
||
type Or = A | B; | ||
>Or : Symbol(Or, Decl(typePredicatesInUnion.ts, 5, 1)) | ||
>A : Symbol(A, Decl(typePredicatesInUnion.ts, 0, 0)) | ||
>B : Symbol(B, Decl(typePredicatesInUnion.ts, 2, 1)) | ||
|
||
function f(o: Or, x: {}) { | ||
>f : Symbol(f, Decl(typePredicatesInUnion.ts, 7, 16)) | ||
>o : Symbol(o, Decl(typePredicatesInUnion.ts, 9, 11)) | ||
>Or : Symbol(Or, Decl(typePredicatesInUnion.ts, 5, 1)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 9, 17)) | ||
|
||
if (o.pred(x)) { | ||
>o.pred : Symbol(pred, Decl(typePredicatesInUnion.ts, 0, 13), Decl(typePredicatesInUnion.ts, 3, 13)) | ||
>o : Symbol(o, Decl(typePredicatesInUnion.ts, 9, 11)) | ||
>pred : Symbol(pred, Decl(typePredicatesInUnion.ts, 0, 13), Decl(typePredicatesInUnion.ts, 3, 13)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 9, 17)) | ||
|
||
x; | ||
>x : Symbol(x, Decl(typePredicatesInUnion.ts, 9, 17)) | ||
} | ||
} | ||
|
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,41 @@ | ||
=== tests/cases/compiler/typePredicatesInUnion.ts === | ||
interface A { | ||
>A : A | ||
|
||
pred(x: {}): x is boolean; | ||
>pred : (x: {}) => x is boolean | ||
>x : {} | ||
>x : any | ||
} | ||
interface B { | ||
>B : B | ||
|
||
pred(x: {}): x is string; | ||
>pred : (x: {}) => x is string | ||
>x : {} | ||
>x : any | ||
} | ||
|
||
type Or = A | B; | ||
>Or : Or | ||
>A : A | ||
>B : B | ||
|
||
function f(o: Or, x: {}) { | ||
>f : (o: Or, x: {}) => void | ||
>o : Or | ||
>Or : Or | ||
>x : {} | ||
|
||
if (o.pred(x)) { | ||
>o.pred(x) : boolean | ||
>o.pred : ((x: {}) => x is boolean) | ((x: {}) => x is string) | ||
>o : Or | ||
>pred : ((x: {}) => x is boolean) | ((x: {}) => x is string) | ||
>x : {} | ||
|
||
x; | ||
>x : string | boolean | ||
} | ||
} | ||
|
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,11 @@ | ||
//// [typePredicatesInUnion2.ts] | ||
declare function isString(x: any): x is string; | ||
declare function isNumber(x: any): x is number; | ||
declare function f(p: typeof isString | typeof isNumber): void; | ||
f(isString); | ||
f(isNumber); | ||
|
||
|
||
//// [typePredicatesInUnion2.js] | ||
f(isString); | ||
f(isNumber); |
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,25 @@ | ||
=== tests/cases/compiler/typePredicatesInUnion2.ts === | ||
declare function isString(x: any): x is string; | ||
>isString : Symbol(isString, Decl(typePredicatesInUnion2.ts, 0, 0)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion2.ts, 0, 26)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion2.ts, 0, 26)) | ||
|
||
declare function isNumber(x: any): x is number; | ||
>isNumber : Symbol(isNumber, Decl(typePredicatesInUnion2.ts, 0, 47)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion2.ts, 1, 26)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion2.ts, 1, 26)) | ||
|
||
declare function f(p: typeof isString | typeof isNumber): void; | ||
>f : Symbol(f, Decl(typePredicatesInUnion2.ts, 1, 47)) | ||
>p : Symbol(p, Decl(typePredicatesInUnion2.ts, 2, 19)) | ||
>isString : Symbol(isString, Decl(typePredicatesInUnion2.ts, 0, 0)) | ||
>isNumber : Symbol(isNumber, Decl(typePredicatesInUnion2.ts, 0, 47)) | ||
|
||
f(isString); | ||
>f : Symbol(f, Decl(typePredicatesInUnion2.ts, 1, 47)) | ||
>isString : Symbol(isString, Decl(typePredicatesInUnion2.ts, 0, 0)) | ||
|
||
f(isNumber); | ||
>f : Symbol(f, Decl(typePredicatesInUnion2.ts, 1, 47)) | ||
>isNumber : Symbol(isNumber, Decl(typePredicatesInUnion2.ts, 0, 47)) | ||
|
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,27 @@ | ||
=== tests/cases/compiler/typePredicatesInUnion2.ts === | ||
declare function isString(x: any): x is string; | ||
>isString : (x: any) => x is string | ||
>x : any | ||
>x : any | ||
|
||
declare function isNumber(x: any): x is number; | ||
>isNumber : (x: any) => x is number | ||
>x : any | ||
>x : any | ||
|
||
declare function f(p: typeof isString | typeof isNumber): void; | ||
>f : (p: ((x: any) => x is string) | ((x: any) => x is number)) => void | ||
>p : ((x: any) => x is string) | ((x: any) => x is number) | ||
>isString : (x: any) => x is string | ||
>isNumber : (x: any) => x is number | ||
|
||
f(isString); | ||
>f(isString) : void | ||
>f : (p: ((x: any) => x is string) | ((x: any) => x is number)) => void | ||
>isString : (x: any) => x is string | ||
|
||
f(isNumber); | ||
>f(isNumber) : void | ||
>f : (p: ((x: any) => x is string) | ((x: any) => x is number)) => void | ||
>isNumber : (x: any) => x is number | ||
|
25 changes: 25 additions & 0 deletions
25
tests/baselines/reference/typePredicatesInUnion_noMatch.js
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,25 @@ | ||
//// [typePredicatesInUnion_noMatch.ts] | ||
interface A { | ||
pred(x: {}, y: {}): x is boolean; | ||
} | ||
interface B { | ||
pred(x: {}, y: {}): y is string; | ||
} | ||
|
||
type Or = A | B; | ||
|
||
function f(o: Or, x: {}, y: {}) { | ||
if (o.pred(x, y)) { | ||
x; | ||
y; | ||
} | ||
} | ||
|
||
|
||
//// [typePredicatesInUnion_noMatch.js] | ||
function f(o, x, y) { | ||
if (o.pred(x, y)) { | ||
x; | ||
y; | ||
} | ||
} |
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.
I think this change is correct because only the left hand side of the conditional is a type predicate. @ahejlsberg could you verify, then merge
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.
Something seems fishy here. If you reverse the order of the operands you get the old result. So apparently both types are subtypes of each other, or otherwise subtype reduction would consistently pick one of them. I'm not sure why this is the case, since
(x: any) => x is any[]
should be a subtype of(x: any) => boolean
. You may want to investigate.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.
Added a test -- the return type seems to be
boolean
the other way around as well.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.
Ok, looks good.