• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2class A {
3    propA: number;
4}
5
6class B {
7    propB: number;
8}
9
10class C extends A {
11    propC: number;
12}
13
14function hasANonBooleanReturnStatement(x): x is A {
15    return '';
16}
17
18function hasTypeGuardTypeInsideTypeGuardType(x): x is x is A {
19    return true;
20}
21
22function hasMissingIsKeyword(): x {
23    return true;
24}
25
26function hasMissingParameter(): x is A {
27    return true;
28}
29
30function hasMissingTypeInTypeGuardType(x): x is {
31    return true;
32}
33
34function hasNonMatchingParameter(y): x is A {
35    return true;
36}
37
38function hasNonMatchingParameterType1(x: A): x is B {
39    return true;
40}
41
42function hasNonMatchingParameterType2(x: string): x is number {
43    return true;
44}
45
46function hasNonMathcingGenericType<T>(a: string): a is T[] {
47    return true;
48}
49
50let a: A;
51let b: B;
52
53declare function isB(p1): p1 is B;
54declare function isC(p1): p1 is C;
55declare function funA(p1: any, p2: any): p1 is B;
56declare function hasNoTypeGuard(x);
57
58// Passed argument is not the same as the one being guarded.
59if (isB(b)) {
60    a.propB;
61}
62
63// Parameter index and argument index for the type guard target is not matching.
64if (funA(0, a)) {
65    a.propB; // Error
66}
67
68// No type guard in if statement
69if (hasNoTypeGuard(a)) {
70    a.propB;
71}
72
73// Type predicate type is not assignable
74declare function acceptingDifferentSignatureTypeGuardFunction(p1: (p1) => p1 is B);
75acceptingDifferentSignatureTypeGuardFunction(isC);
76
77// Boolean not assignable to type guard
78var assign1: (p1, p2) => p1 is A;
79assign1 = function(p1, p2): boolean {
80    return true;
81};
82
83// Must have matching parameter index
84var assign2: (p1, p2) => p1 is A;
85assign2 = function(p1, p2): p2 is A {
86    return true;
87};
88
89// No matching signature
90var assign3: (p1, p2) => p1 is A;
91assign3 = function(p1, p2, p3): p1 is A {
92    return true;
93};
94
95// Type predicates in non-return type positions
96var b1: b is A;
97function b2(a: b is A) {};
98function b3(): A | b is A {
99    return true;
100};
101
102// Non-compatiable type predicate positions for signature declarations
103class D {
104    constructor(p1: A): p1 is C {
105        return true;
106    }
107    get m1(p1: A): p1 is C {
108        return true;
109    }
110    set m2(p1: A): p1 is C {
111        return true;
112    }
113}
114
115interface I1 {
116    new (p1: A): p1 is C;
117}
118
119interface I2 {
120    [index: number]: p1 is C;
121}
122
123// Reference to rest parameter
124function b4(...a): a is A {
125    return true;
126}
127
128// Reference to binding pattern
129function b5({a, b, p1}, p2, p3): p1 is A {
130    return true;
131}
132
133function b6([a, b, p1], p2, p3): p1 is A {
134    return true;
135}
136
137function b7({a, b, c: {p1}}, p2, p3): p1 is A {
138    return true;
139}
140
141// Should not crash the compiler
142var x: A;
143if (hasMissingParameter()) {
144    x.propA;
145}
146
147// repro #17297
148
149type Keys = 'a'|'b'|'c'
150type KeySet<T extends Keys> = { [k in T]: true }
151
152// expected an error, since Keys doesn't have a 'd'
153declare function hasKey<T extends Keys>(x: KeySet<T>): x is KeySet<T|'d'>;
154
155type Foo = { 'a': string; }
156type Bar = { 'a': number; }
157
158interface NeedsFoo<T extends Foo> {
159    foo: T;
160    isFoo(): this is NeedsFoo<Bar>; // should error
161};
162
163declare var anError: NeedsFoo<Bar>; // error, as expected
164declare var alsoAnError: NeedsFoo<number>; // also error, as expected
165declare function newError1(x: any): x is NeedsFoo<Bar>; // should error
166declare function newError2(x: any): x is NeedsFoo<number>; // should error
167declare function newError3(x: number): x is NeedsFoo<number>; // should error
168