-
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
Changes from 12 commits
bf6a3f7
b2a29de
a072a64
2d0dd82
304665a
7350faa
2bc75c0
9fe2a59
cadf250
7001e71
d52812f
b384c9c
e72f847
aaa6928
3e1b337
8b6fbd7
267c3a9
8e0b1e9
7c5718c
4a8b449
85d70e4
8d2ea56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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; }); |
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)) | ||
|
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 | ||
|
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; | ||
} | ||
} |
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)) | ||
} | ||
} | ||
|
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 | ||
} | ||
} | ||
|
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
=== tests/cases/compiler/typePredicatesInUnion_noMatch.ts === | ||
interface A { | ||
>A : Symbol(A, Decl(typePredicatesInUnion_noMatch.ts, 0, 0)) | ||
|
||
pred(x: {}, y: {}): x is boolean; | ||
>pred : Symbol(A.pred, Decl(typePredicatesInUnion_noMatch.ts, 0, 13)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion_noMatch.ts, 1, 9)) | ||
>y : Symbol(y, Decl(typePredicatesInUnion_noMatch.ts, 1, 15)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion_noMatch.ts, 1, 9)) | ||
} | ||
interface B { | ||
>B : Symbol(B, Decl(typePredicatesInUnion_noMatch.ts, 2, 1)) | ||
|
||
pred(x: {}, y: {}): y is string; | ||
>pred : Symbol(B.pred, Decl(typePredicatesInUnion_noMatch.ts, 3, 13)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion_noMatch.ts, 4, 9)) | ||
>y : Symbol(y, Decl(typePredicatesInUnion_noMatch.ts, 4, 15)) | ||
>y : Symbol(y, Decl(typePredicatesInUnion_noMatch.ts, 4, 15)) | ||
} | ||
|
||
type Or = A | B; | ||
>Or : Symbol(Or, Decl(typePredicatesInUnion_noMatch.ts, 5, 1)) | ||
>A : Symbol(A, Decl(typePredicatesInUnion_noMatch.ts, 0, 0)) | ||
>B : Symbol(B, Decl(typePredicatesInUnion_noMatch.ts, 2, 1)) | ||
|
||
function f(o: Or, x: {}, y: {}) { | ||
>f : Symbol(f, Decl(typePredicatesInUnion_noMatch.ts, 7, 16)) | ||
>o : Symbol(o, Decl(typePredicatesInUnion_noMatch.ts, 9, 11)) | ||
>Or : Symbol(Or, Decl(typePredicatesInUnion_noMatch.ts, 5, 1)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion_noMatch.ts, 9, 17)) | ||
>y : Symbol(y, Decl(typePredicatesInUnion_noMatch.ts, 9, 24)) | ||
|
||
if (o.pred(x, y)) { | ||
>o.pred : Symbol(pred, Decl(typePredicatesInUnion_noMatch.ts, 0, 13), Decl(typePredicatesInUnion_noMatch.ts, 3, 13)) | ||
>o : Symbol(o, Decl(typePredicatesInUnion_noMatch.ts, 9, 11)) | ||
>pred : Symbol(pred, Decl(typePredicatesInUnion_noMatch.ts, 0, 13), Decl(typePredicatesInUnion_noMatch.ts, 3, 13)) | ||
>x : Symbol(x, Decl(typePredicatesInUnion_noMatch.ts, 9, 17)) | ||
>y : Symbol(y, Decl(typePredicatesInUnion_noMatch.ts, 9, 24)) | ||
|
||
x; | ||
>x : Symbol(x, Decl(typePredicatesInUnion_noMatch.ts, 9, 17)) | ||
|
||
y; | ||
>y : Symbol(y, Decl(typePredicatesInUnion_noMatch.ts, 9, 24)) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
=== tests/cases/compiler/typePredicatesInUnion_noMatch.ts === | ||
interface A { | ||
>A : A | ||
|
||
pred(x: {}, y: {}): x is boolean; | ||
>pred : (x: {}, y: {}) => x is boolean | ||
>x : {} | ||
>y : {} | ||
>x : any | ||
} | ||
interface B { | ||
>B : B | ||
|
||
pred(x: {}, y: {}): y is string; | ||
>pred : (x: {}, y: {}) => y is string | ||
>x : {} | ||
>y : {} | ||
>y : any | ||
} | ||
|
||
type Or = A | B; | ||
>Or : Or | ||
>A : A | ||
>B : B | ||
|
||
function f(o: Or, x: {}, y: {}) { | ||
>f : (o: Or, x: {}, y: {}) => void | ||
>o : Or | ||
>Or : Or | ||
>x : {} | ||
>y : {} | ||
|
||
if (o.pred(x, y)) { | ||
>o.pred(x, y) : boolean | ||
>o.pred : ((x: {}, y: {}) => x is boolean) | ((x: {}, y: {}) => y is string) | ||
>o : Or | ||
>pred : ((x: {}, y: {}) => x is boolean) | ((x: {}, y: {}) => y is string) | ||
>x : {} | ||
>y : {} | ||
|
||
x; | ||
>x : {} | ||
|
||
y; | ||
>y : {} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare function f<T>(predicate: (x: {}) => x is T): T; | ||
// 'res' should be of type 'number'. | ||
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. change this line to 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. I didn't want us taking any contextual type -- presumably we should be doing that on assignments if we're not already. |
||
const res = f((n): n is number => true); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
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)) { | ||
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. interesting.. why doesn't this match? 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. oh, it's because you can't make a single type predicate that narrows two variables. |
||
x; | ||
y; | ||
} | ||
} |
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.
This link is a bet that our use of github will last as long as the typescript source. Probably a decent bet, but I personally think the explanation stands on its own as a valid justification.
(Same for the use of github bug numbers earlier in the review.)