-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infer non-narrowing predicates when the contextual signature is a typ…
…e predicate
- Loading branch information
Showing
5 changed files
with
343 additions
and
26 deletions.
There are no files selected for viewing
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
36 changes: 36 additions & 0 deletions
36
tests/baselines/reference/inferContextualTypePredicates1.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,36 @@ | ||
inferContextualTypePredicates1.ts(13,26): error TS2345: Argument of type '(item: Foo | Bar) => false' is not assignable to parameter of type '(a: Foo | Bar) => a is Foo | Bar'. | ||
Signature '(item: Foo | Bar): false' must be a type predicate. | ||
inferContextualTypePredicates1.ts(14,26): error TS2345: Argument of type '(item: Foo | Bar) => true' is not assignable to parameter of type '(a: Foo | Bar) => a is Foo | Bar'. | ||
Signature '(item: Foo | Bar): true' must be a type predicate. | ||
inferContextualTypePredicates1.ts(17,7): error TS2322: Type '(a: string | null, b: string | null) => boolean' is not assignable to type '(a: string | null, b: string | null) => b is string'. | ||
Signature '(a: string | null, b: string | null): boolean' must be a type predicate. | ||
|
||
|
||
==== inferContextualTypePredicates1.ts (3 errors) ==== | ||
type Foo = { type: "foo"; foo: number }; | ||
type Bar = { type: "bar"; bar: string }; | ||
|
||
declare function skipIf<A, B extends A>( | ||
as: A[], | ||
predicate: (a: A) => a is B, | ||
): Exclude<A, B>[]; | ||
|
||
declare const items: (Foo | Bar)[]; | ||
|
||
const r1 = skipIf(items, (item) => item.type === "foo"); // ok | ||
const r2 = skipIf(items, (item) => item.type === "foo" || item.type === "bar"); // ok | ||
const r3 = skipIf(items, (item) => false); // error | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS2345: Argument of type '(item: Foo | Bar) => false' is not assignable to parameter of type '(a: Foo | Bar) => a is Foo | Bar'. | ||
!!! error TS2345: Signature '(item: Foo | Bar): false' must be a type predicate. | ||
const r4 = skipIf(items, (item) => true); // error | ||
~~~~~~~~~~~~~~ | ||
!!! error TS2345: Argument of type '(item: Foo | Bar) => true' is not assignable to parameter of type '(a: Foo | Bar) => a is Foo | Bar'. | ||
!!! error TS2345: Signature '(item: Foo | Bar): true' must be a type predicate. | ||
|
||
const pred1: (a: string | null, b: string | null) => b is string = (a, b) => typeof b === 'string'; // ok | ||
const pred2: (a: string | null, b: string | null) => b is string = (a, b) => typeof a === 'string'; // error | ||
~~~~~ | ||
!!! error TS2322: Type '(a: string | null, b: string | null) => boolean' is not assignable to type '(a: string | null, b: string | null) => b is string'. | ||
!!! error TS2322: Signature '(a: string | null, b: string | null): boolean' must be a type predicate. | ||
|
91 changes: 91 additions & 0 deletions
91
tests/baselines/reference/inferContextualTypePredicates1.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,91 @@ | ||
//// [tests/cases/compiler/inferContextualTypePredicates1.ts] //// | ||
|
||
=== inferContextualTypePredicates1.ts === | ||
type Foo = { type: "foo"; foo: number }; | ||
>Foo : Symbol(Foo, Decl(inferContextualTypePredicates1.ts, 0, 0)) | ||
>type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 0, 12)) | ||
>foo : Symbol(foo, Decl(inferContextualTypePredicates1.ts, 0, 25)) | ||
|
||
type Bar = { type: "bar"; bar: string }; | ||
>Bar : Symbol(Bar, Decl(inferContextualTypePredicates1.ts, 0, 40)) | ||
>type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 1, 12)) | ||
>bar : Symbol(bar, Decl(inferContextualTypePredicates1.ts, 1, 25)) | ||
|
||
declare function skipIf<A, B extends A>( | ||
>skipIf : Symbol(skipIf, Decl(inferContextualTypePredicates1.ts, 1, 40)) | ||
>A : Symbol(A, Decl(inferContextualTypePredicates1.ts, 3, 24)) | ||
>B : Symbol(B, Decl(inferContextualTypePredicates1.ts, 3, 26)) | ||
>A : Symbol(A, Decl(inferContextualTypePredicates1.ts, 3, 24)) | ||
|
||
as: A[], | ||
>as : Symbol(as, Decl(inferContextualTypePredicates1.ts, 3, 40)) | ||
>A : Symbol(A, Decl(inferContextualTypePredicates1.ts, 3, 24)) | ||
|
||
predicate: (a: A) => a is B, | ||
>predicate : Symbol(predicate, Decl(inferContextualTypePredicates1.ts, 4, 10)) | ||
>a : Symbol(a, Decl(inferContextualTypePredicates1.ts, 5, 14)) | ||
>A : Symbol(A, Decl(inferContextualTypePredicates1.ts, 3, 24)) | ||
>a : Symbol(a, Decl(inferContextualTypePredicates1.ts, 5, 14)) | ||
>B : Symbol(B, Decl(inferContextualTypePredicates1.ts, 3, 26)) | ||
|
||
): Exclude<A, B>[]; | ||
>Exclude : Symbol(Exclude, Decl(lib.es5.d.ts, --, --)) | ||
>A : Symbol(A, Decl(inferContextualTypePredicates1.ts, 3, 24)) | ||
>B : Symbol(B, Decl(inferContextualTypePredicates1.ts, 3, 26)) | ||
|
||
declare const items: (Foo | Bar)[]; | ||
>items : Symbol(items, Decl(inferContextualTypePredicates1.ts, 8, 13)) | ||
>Foo : Symbol(Foo, Decl(inferContextualTypePredicates1.ts, 0, 0)) | ||
>Bar : Symbol(Bar, Decl(inferContextualTypePredicates1.ts, 0, 40)) | ||
|
||
const r1 = skipIf(items, (item) => item.type === "foo"); // ok | ||
>r1 : Symbol(r1, Decl(inferContextualTypePredicates1.ts, 10, 5)) | ||
>skipIf : Symbol(skipIf, Decl(inferContextualTypePredicates1.ts, 1, 40)) | ||
>items : Symbol(items, Decl(inferContextualTypePredicates1.ts, 8, 13)) | ||
>item : Symbol(item, Decl(inferContextualTypePredicates1.ts, 10, 26)) | ||
>item.type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 0, 12), Decl(inferContextualTypePredicates1.ts, 1, 12)) | ||
>item : Symbol(item, Decl(inferContextualTypePredicates1.ts, 10, 26)) | ||
>type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 0, 12), Decl(inferContextualTypePredicates1.ts, 1, 12)) | ||
|
||
const r2 = skipIf(items, (item) => item.type === "foo" || item.type === "bar"); // ok | ||
>r2 : Symbol(r2, Decl(inferContextualTypePredicates1.ts, 11, 5)) | ||
>skipIf : Symbol(skipIf, Decl(inferContextualTypePredicates1.ts, 1, 40)) | ||
>items : Symbol(items, Decl(inferContextualTypePredicates1.ts, 8, 13)) | ||
>item : Symbol(item, Decl(inferContextualTypePredicates1.ts, 11, 26)) | ||
>item.type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 0, 12), Decl(inferContextualTypePredicates1.ts, 1, 12)) | ||
>item : Symbol(item, Decl(inferContextualTypePredicates1.ts, 11, 26)) | ||
>type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 0, 12), Decl(inferContextualTypePredicates1.ts, 1, 12)) | ||
>item.type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 1, 12)) | ||
>item : Symbol(item, Decl(inferContextualTypePredicates1.ts, 11, 26)) | ||
>type : Symbol(type, Decl(inferContextualTypePredicates1.ts, 1, 12)) | ||
|
||
const r3 = skipIf(items, (item) => false); // error | ||
>r3 : Symbol(r3, Decl(inferContextualTypePredicates1.ts, 12, 5)) | ||
>skipIf : Symbol(skipIf, Decl(inferContextualTypePredicates1.ts, 1, 40)) | ||
>items : Symbol(items, Decl(inferContextualTypePredicates1.ts, 8, 13)) | ||
>item : Symbol(item, Decl(inferContextualTypePredicates1.ts, 12, 26)) | ||
|
||
const r4 = skipIf(items, (item) => true); // error | ||
>r4 : Symbol(r4, Decl(inferContextualTypePredicates1.ts, 13, 5)) | ||
>skipIf : Symbol(skipIf, Decl(inferContextualTypePredicates1.ts, 1, 40)) | ||
>items : Symbol(items, Decl(inferContextualTypePredicates1.ts, 8, 13)) | ||
>item : Symbol(item, Decl(inferContextualTypePredicates1.ts, 13, 26)) | ||
|
||
const pred1: (a: string | null, b: string | null) => b is string = (a, b) => typeof b === 'string'; // ok | ||
>pred1 : Symbol(pred1, Decl(inferContextualTypePredicates1.ts, 15, 5)) | ||
>a : Symbol(a, Decl(inferContextualTypePredicates1.ts, 15, 14)) | ||
>b : Symbol(b, Decl(inferContextualTypePredicates1.ts, 15, 31)) | ||
>b : Symbol(b, Decl(inferContextualTypePredicates1.ts, 15, 31)) | ||
>a : Symbol(a, Decl(inferContextualTypePredicates1.ts, 15, 68)) | ||
>b : Symbol(b, Decl(inferContextualTypePredicates1.ts, 15, 70)) | ||
>b : Symbol(b, Decl(inferContextualTypePredicates1.ts, 15, 70)) | ||
|
||
const pred2: (a: string | null, b: string | null) => b is string = (a, b) => typeof a === 'string'; // error | ||
>pred2 : Symbol(pred2, Decl(inferContextualTypePredicates1.ts, 16, 5)) | ||
>a : Symbol(a, Decl(inferContextualTypePredicates1.ts, 16, 14)) | ||
>b : Symbol(b, Decl(inferContextualTypePredicates1.ts, 16, 31)) | ||
>b : Symbol(b, Decl(inferContextualTypePredicates1.ts, 16, 31)) | ||
>a : Symbol(a, Decl(inferContextualTypePredicates1.ts, 16, 68)) | ||
>b : Symbol(b, Decl(inferContextualTypePredicates1.ts, 16, 70)) | ||
>a : Symbol(a, Decl(inferContextualTypePredicates1.ts, 16, 68)) | ||
|
175 changes: 175 additions & 0 deletions
175
tests/baselines/reference/inferContextualTypePredicates1.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,175 @@ | ||
//// [tests/cases/compiler/inferContextualTypePredicates1.ts] //// | ||
|
||
=== inferContextualTypePredicates1.ts === | ||
type Foo = { type: "foo"; foo: number }; | ||
>Foo : Foo | ||
> : ^^^ | ||
>type : "foo" | ||
> : ^^^^^ | ||
>foo : number | ||
> : ^^^^^^ | ||
|
||
type Bar = { type: "bar"; bar: string }; | ||
>Bar : Bar | ||
> : ^^^ | ||
>type : "bar" | ||
> : ^^^^^ | ||
>bar : string | ||
> : ^^^^^^ | ||
|
||
declare function skipIf<A, B extends A>( | ||
>skipIf : <A, B extends A>(as: A[], predicate: (a: A) => a is B) => Exclude<A, B>[] | ||
> : ^ ^^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ | ||
|
||
as: A[], | ||
>as : A[] | ||
> : ^^^ | ||
|
||
predicate: (a: A) => a is B, | ||
>predicate : (a: A) => a is B | ||
> : ^ ^^ ^^^^^ | ||
>a : A | ||
> : ^ | ||
|
||
): Exclude<A, B>[]; | ||
|
||
declare const items: (Foo | Bar)[]; | ||
>items : (Foo | Bar)[] | ||
> : ^^^^^^^^^^^^^ | ||
|
||
const r1 = skipIf(items, (item) => item.type === "foo"); // ok | ||
>r1 : Bar[] | ||
> : ^^^^^ | ||
>skipIf(items, (item) => item.type === "foo") : Bar[] | ||
> : ^^^^^ | ||
>skipIf : <A, B extends A>(as: A[], predicate: (a: A) => a is B) => Exclude<A, B>[] | ||
> : ^ ^^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ | ||
>items : (Foo | Bar)[] | ||
> : ^^^^^^^^^^^^^ | ||
>(item) => item.type === "foo" : (item: Foo | Bar) => item is Foo | ||
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>item : Foo | Bar | ||
> : ^^^^^^^^^ | ||
>item.type === "foo" : boolean | ||
> : ^^^^^^^ | ||
>item.type : "foo" | "bar" | ||
> : ^^^^^^^^^^^^^ | ||
>item : Foo | Bar | ||
> : ^^^^^^^^^ | ||
>type : "foo" | "bar" | ||
> : ^^^^^^^^^^^^^ | ||
>"foo" : "foo" | ||
> : ^^^^^ | ||
|
||
const r2 = skipIf(items, (item) => item.type === "foo" || item.type === "bar"); // ok | ||
>r2 : never[] | ||
> : ^^^^^^^ | ||
>skipIf(items, (item) => item.type === "foo" || item.type === "bar") : never[] | ||
> : ^^^^^^^ | ||
>skipIf : <A, B extends A>(as: A[], predicate: (a: A) => a is B) => Exclude<A, B>[] | ||
> : ^ ^^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ | ||
>items : (Foo | Bar)[] | ||
> : ^^^^^^^^^^^^^ | ||
>(item) => item.type === "foo" || item.type === "bar" : (item: Foo | Bar) => item is Foo | Bar | ||
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>item : Foo | Bar | ||
> : ^^^^^^^^^ | ||
>item.type === "foo" || item.type === "bar" : boolean | ||
> : ^^^^^^^ | ||
>item.type === "foo" : boolean | ||
> : ^^^^^^^ | ||
>item.type : "foo" | "bar" | ||
> : ^^^^^^^^^^^^^ | ||
>item : Foo | Bar | ||
> : ^^^^^^^^^ | ||
>type : "foo" | "bar" | ||
> : ^^^^^^^^^^^^^ | ||
>"foo" : "foo" | ||
> : ^^^^^ | ||
>item.type === "bar" : boolean | ||
> : ^^^^^^^ | ||
>item.type : "bar" | ||
> : ^^^^^ | ||
>item : Bar | ||
> : ^^^ | ||
>type : "bar" | ||
> : ^^^^^ | ||
>"bar" : "bar" | ||
> : ^^^^^ | ||
|
||
const r3 = skipIf(items, (item) => false); // error | ||
>r3 : never[] | ||
> : ^^^^^^^ | ||
>skipIf(items, (item) => false) : never[] | ||
> : ^^^^^^^ | ||
>skipIf : <A, B extends A>(as: A[], predicate: (a: A) => a is B) => Exclude<A, B>[] | ||
> : ^ ^^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ | ||
>items : (Foo | Bar)[] | ||
> : ^^^^^^^^^^^^^ | ||
>(item) => false : (item: Foo | Bar) => false | ||
> : ^ ^^^^^^^^^^^^^^^^^^^^^ | ||
>item : Foo | Bar | ||
> : ^^^^^^^^^ | ||
>false : false | ||
> : ^^^^^ | ||
|
||
const r4 = skipIf(items, (item) => true); // error | ||
>r4 : never[] | ||
> : ^^^^^^^ | ||
>skipIf(items, (item) => true) : never[] | ||
> : ^^^^^^^ | ||
>skipIf : <A, B extends A>(as: A[], predicate: (a: A) => a is B) => Exclude<A, B>[] | ||
> : ^ ^^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^ | ||
>items : (Foo | Bar)[] | ||
> : ^^^^^^^^^^^^^ | ||
>(item) => true : (item: Foo | Bar) => true | ||
> : ^ ^^^^^^^^^^^^^^^^^^^^ | ||
>item : Foo | Bar | ||
> : ^^^^^^^^^ | ||
>true : true | ||
> : ^^^^ | ||
|
||
const pred1: (a: string | null, b: string | null) => b is string = (a, b) => typeof b === 'string'; // ok | ||
>pred1 : (a: string | null, b: string | null) => b is string | ||
> : ^ ^^ ^^ ^^ ^^^^^ | ||
>a : string | null | ||
> : ^^^^^^^^^^^^^ | ||
>b : string | null | ||
> : ^^^^^^^^^^^^^ | ||
>(a, b) => typeof b === 'string' : (a: string | null, b: string | null) => b is string | ||
> : ^ ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>a : string | null | ||
> : ^^^^^^^^^^^^^ | ||
>b : string | null | ||
> : ^^^^^^^^^^^^^ | ||
>typeof b === 'string' : boolean | ||
> : ^^^^^^^ | ||
>typeof b : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>b : string | null | ||
> : ^^^^^^^^^^^^^ | ||
>'string' : "string" | ||
> : ^^^^^^^^ | ||
|
||
const pred2: (a: string | null, b: string | null) => b is string = (a, b) => typeof a === 'string'; // error | ||
>pred2 : (a: string | null, b: string | null) => b is string | ||
> : ^ ^^ ^^ ^^ ^^^^^ | ||
>a : string | null | ||
> : ^^^^^^^^^^^^^ | ||
>b : string | null | ||
> : ^^^^^^^^^^^^^ | ||
>(a, b) => typeof a === 'string' : (a: string | null, b: string | null) => boolean | ||
> : ^ ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>a : string | null | ||
> : ^^^^^^^^^^^^^ | ||
>b : string | null | ||
> : ^^^^^^^^^^^^^ | ||
>typeof a === 'string' : boolean | ||
> : ^^^^^^^ | ||
>typeof a : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>a : string | null | ||
> : ^^^^^^^^^^^^^ | ||
>'string' : "string" | ||
> : ^^^^^^^^ | ||
|
Oops, something went wrong.