Skip to content

Commit 0654fa2

Browse files
committed
Added tests
1 parent e52ed1a commit 0654fa2

File tree

3 files changed

+112
-11
lines changed

3 files changed

+112
-11
lines changed

tests/baselines/reference/typeGuardFunctionErrors.errors.txt

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,21 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(123,20
6262
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(128,34): error TS1230: A type predicate cannot reference element 'p1' in a binding pattern.
6363
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(132,34): error TS1230: A type predicate cannot reference element 'p1' in a binding pattern.
6464
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(136,39): error TS1230: A type predicate cannot reference element 'p1' in a binding pattern.
65+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(152,68): error TS2344: Type 'T | "d"' does not satisfy the constraint 'Keys'.
66+
Type '"d"' is not assignable to type 'Keys'.
67+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(159,31): error TS2344: Type 'Bar' does not satisfy the constraint 'Foo'.
68+
Types of property ''a'' are incompatible.
69+
Type 'number' is not assignable to type 'string'.
70+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(162,31): error TS2344: Type 'Bar' does not satisfy the constraint 'Foo'.
71+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(163,35): error TS2344: Type 'number' does not satisfy the constraint 'Foo'.
72+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(164,51): error TS2344: Type 'Bar' does not satisfy the constraint 'Foo'.
73+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(165,51): error TS2344: Type 'number' does not satisfy the constraint 'Foo'.
74+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(166,45): error TS2677: A type predicate's type must be assignable to its parameter's type.
75+
Type 'NeedsFoo<number>' is not assignable to type 'number'.
76+
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(166,54): error TS2344: Type 'number' does not satisfy the constraint 'Foo'.
6577

6678

67-
==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (54 errors) ====
79+
==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (62 errors) ====
6880
class A {
6981
~
7082
!!! error TS2300: Duplicate identifier 'A'.
@@ -175,7 +187,7 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(136,39
175187

176188
// No type guard in if statement
177189
if (hasNoTypeGuard(a)) {
178-
a.propB;
190+
a.propB;
179191
~~~~~
180192
!!! error TS2551: Property 'propB' does not exist on type 'A'. Did you mean 'propA'?
181193
}
@@ -208,7 +220,7 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(136,39
208220
return true;
209221
};
210222

211-
// No matching signature
223+
// No matching signature
212224
var assign3: (p1, p2) => p1 is A;
213225
assign3 = function(p1, p2, p3): p1 is A {
214226
~~~~~~~
@@ -326,4 +338,47 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(136,39
326338
var x: A;
327339
if (hasMissingParameter()) {
328340
x.propA;
329-
}
341+
}
342+
343+
// repro #17297
344+
345+
type Keys = 'a'|'b'|'c'
346+
type KeySet<T extends Keys> = { [k in T]: true }
347+
348+
// expected an error, since Keys doesn't have a 'd'
349+
declare function hasKey<T extends Keys>(x: KeySet<T>): x is KeySet<T|'d'>;
350+
~~~~~
351+
!!! error TS2344: Type 'T | "d"' does not satisfy the constraint 'Keys'.
352+
!!! error TS2344: Type '"d"' is not assignable to type 'Keys'.
353+
354+
type Foo = { 'a': string; }
355+
type Bar = { 'a': number; }
356+
357+
interface NeedsFoo<T extends Foo> {
358+
foo: T;
359+
isFoo(): this is NeedsFoo<Bar>; // should error
360+
~~~
361+
!!! error TS2344: Type 'Bar' does not satisfy the constraint 'Foo'.
362+
!!! error TS2344: Types of property ''a'' are incompatible.
363+
!!! error TS2344: Type 'number' is not assignable to type 'string'.
364+
};
365+
366+
declare var anError: NeedsFoo<Bar>; // error, as expected
367+
~~~
368+
!!! error TS2344: Type 'Bar' does not satisfy the constraint 'Foo'.
369+
declare var alsoAnError: NeedsFoo<number>; // also error, as expected
370+
~~~~~~
371+
!!! error TS2344: Type 'number' does not satisfy the constraint 'Foo'.
372+
declare function newError1(x: any): x is NeedsFoo<Bar>; // should error
373+
~~~
374+
!!! error TS2344: Type 'Bar' does not satisfy the constraint 'Foo'.
375+
declare function newError2(x: any): x is NeedsFoo<number>; // should error
376+
~~~~~~
377+
!!! error TS2344: Type 'number' does not satisfy the constraint 'Foo'.
378+
declare function newError3(x: number): x is NeedsFoo<number>; // should error
379+
~~~~~~~~~~~~~~~~
380+
!!! error TS2677: A type predicate's type must be assignable to its parameter's type.
381+
!!! error TS2677: Type 'NeedsFoo<number>' is not assignable to type 'number'.
382+
~~~~~~
383+
!!! error TS2344: Type 'number' does not satisfy the constraint 'Foo'.
384+

tests/baselines/reference/typeGuardFunctionErrors.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ if (funA(0, a)) {
6767

6868
// No type guard in if statement
6969
if (hasNoTypeGuard(a)) {
70-
a.propB;
70+
a.propB;
7171
}
7272

7373
// Type predicate type is not assignable
@@ -86,7 +86,7 @@ assign2 = function(p1, p2): p2 is A {
8686
return true;
8787
};
8888

89-
// No matching signature
89+
// No matching signature
9090
var assign3: (p1, p2) => p1 is A;
9191
assign3 = function(p1, p2, p3): p1 is A {
9292
return true;
@@ -142,7 +142,30 @@ function b7({a, b, c: {p1}}, p2, p3): p1 is A {
142142
var x: A;
143143
if (hasMissingParameter()) {
144144
x.propA;
145-
}
145+
}
146+
147+
// repro #17297
148+
149+
type Keys = 'a'|'b'|'c'
150+
type KeySet<T extends Keys> = { [k in T]: true }
151+
152+
// expected an error, since Keys doesn't have a 'd'
153+
declare function hasKey<T extends Keys>(x: KeySet<T>): x is KeySet<T|'d'>;
154+
155+
type Foo = { 'a': string; }
156+
type Bar = { 'a': number; }
157+
158+
interface NeedsFoo<T extends Foo> {
159+
foo: T;
160+
isFoo(): this is NeedsFoo<Bar>; // should error
161+
};
162+
163+
declare var anError: NeedsFoo<Bar>; // error, as expected
164+
declare var alsoAnError: NeedsFoo<number>; // also error, as expected
165+
declare function newError1(x: any): x is NeedsFoo<Bar>; // should error
166+
declare function newError2(x: any): x is NeedsFoo<number>; // should error
167+
declare function newError3(x: number): x is NeedsFoo<number>; // should error
168+
146169

147170
//// [typeGuardFunctionErrors.js]
148171
var __extends = (this && this.__extends) || (function () {
@@ -224,7 +247,7 @@ var assign2;
224247
assign2 = function (p1, p2) {
225248
return true;
226249
};
227-
// No matching signature
250+
// No matching signature
228251
var assign3;
229252
assign3 = function (p1, p2, p3) {
230253
return true;
@@ -290,3 +313,4 @@ var x;
290313
if (hasMissingParameter()) {
291314
x.propA;
292315
}
316+
;

tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ if (funA(0, a)) {
6767

6868
// No type guard in if statement
6969
if (hasNoTypeGuard(a)) {
70-
a.propB;
70+
a.propB;
7171
}
7272

7373
// Type predicate type is not assignable
@@ -86,7 +86,7 @@ assign2 = function(p1, p2): p2 is A {
8686
return true;
8787
};
8888

89-
// No matching signature
89+
// No matching signature
9090
var assign3: (p1, p2) => p1 is A;
9191
assign3 = function(p1, p2, p3): p1 is A {
9292
return true;
@@ -142,4 +142,26 @@ function b7({a, b, c: {p1}}, p2, p3): p1 is A {
142142
var x: A;
143143
if (hasMissingParameter()) {
144144
x.propA;
145-
}
145+
}
146+
147+
// repro #17297
148+
149+
type Keys = 'a'|'b'|'c'
150+
type KeySet<T extends Keys> = { [k in T]: true }
151+
152+
// expected an error, since Keys doesn't have a 'd'
153+
declare function hasKey<T extends Keys>(x: KeySet<T>): x is KeySet<T|'d'>;
154+
155+
type Foo = { 'a': string; }
156+
type Bar = { 'a': number; }
157+
158+
interface NeedsFoo<T extends Foo> {
159+
foo: T;
160+
isFoo(): this is NeedsFoo<Bar>; // should error
161+
};
162+
163+
declare var anError: NeedsFoo<Bar>; // error, as expected
164+
declare var alsoAnError: NeedsFoo<number>; // also error, as expected
165+
declare function newError1(x: any): x is NeedsFoo<Bar>; // should error
166+
declare function newError2(x: any): x is NeedsFoo<number>; // should error
167+
declare function newError3(x: number): x is NeedsFoo<number>; // should error

0 commit comments

Comments
 (0)