File: typeGuardFunctionErrors.types

package info (click to toggle)
node-typescript 3.3.3333-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 324,548 kB
  • sloc: makefile: 6; sh: 3
file content (391 lines) | stat: -rw-r--r-- 7,705 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
=== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts ===
class A {
>A : A

    propA: number;
>propA : number
}

class B {
>B : B

    propB: number;
>propB : number
}

class C extends A {
>C : C
>A : A

    propC: number;
>propC : number
}

function hasANonBooleanReturnStatement(x): x is A {
>hasANonBooleanReturnStatement : (x: any) => x is A
>x : any

    return '';
>'' : ""
}

function hasTypeGuardTypeInsideTypeGuardType(x): x is x is A {
>hasTypeGuardTypeInsideTypeGuardType : (x: any) => x is any
>x : any
>is : any
>A : typeof A

    return true;
>true : true
}

function hasMissingIsKeyword(): x {
>hasMissingIsKeyword : () => any

    return true;
>true : true
}

function hasMissingParameter(): x is A {
>hasMissingParameter : () => x is A

    return true;
>true : true
}

function hasMissingTypeInTypeGuardType(x): x is {
>hasMissingTypeInTypeGuardType : (x: any) => x is {}
>x : any

    return true;
>true : true
}

function hasNonMatchingParameter(y): x is A {
>hasNonMatchingParameter : (y: any) => x is A
>y : any

    return true;
>true : true
}

function hasNonMatchingParameterType1(x: A): x is B {
>hasNonMatchingParameterType1 : (x: A) => x is B
>x : A

    return true;
>true : true
}

function hasNonMatchingParameterType2(x: string): x is number {
>hasNonMatchingParameterType2 : (x: string) => x is number
>x : string

    return true;
>true : true
}

function hasNonMathcingGenericType<T>(a: string): a is T[] {
>hasNonMathcingGenericType : <T>(a: string) => a is T[]
>a : string

    return true;
>true : true
}

let a: A;
>a : A

let b: B;
>b : B

declare function isB(p1): p1 is B;
>isB : (p1: any) => p1 is B
>p1 : any

declare function isC(p1): p1 is C;
>isC : (p1: any) => p1 is C
>p1 : any

declare function funA(p1: any, p2: any): p1 is B;
>funA : (p1: any, p2: any) => p1 is B
>p1 : any
>p2 : any

declare function hasNoTypeGuard(x);
>hasNoTypeGuard : (x: any) => any
>x : any

// Passed argument is not the same as the one being guarded.
if (isB(b)) {
>isB(b) : boolean
>isB : (p1: any) => p1 is B
>b : B

    a.propB;
>a.propB : any
>a : A
>propB : any
}

// Parameter index and argument index for the type guard target is not matching.
if (funA(0, a)) {
>funA(0, a) : boolean
>funA : (p1: any, p2: any) => p1 is B
>0 : 0
>a : A

    a.propB; // Error
>a.propB : any
>a : A
>propB : any
}

// No type guard in if statement
if (hasNoTypeGuard(a)) {
>hasNoTypeGuard(a) : any
>hasNoTypeGuard : (x: any) => any
>a : A

    a.propB;
>a.propB : any
>a : A
>propB : any
}

// Type predicate type is not assignable
declare function acceptingDifferentSignatureTypeGuardFunction(p1: (p1) => p1 is B);
>acceptingDifferentSignatureTypeGuardFunction : (p1: (p1: any) => p1 is B) => any
>p1 : (p1: any) => p1 is B
>p1 : any

acceptingDifferentSignatureTypeGuardFunction(isC);
>acceptingDifferentSignatureTypeGuardFunction(isC) : any
>acceptingDifferentSignatureTypeGuardFunction : (p1: (p1: any) => p1 is B) => any
>isC : (p1: any) => p1 is C

// Boolean not assignable to type guard
var assign1: (p1, p2) => p1 is A;
>assign1 : (p1: any, p2: any) => p1 is A
>p1 : any
>p2 : any

assign1 = function(p1, p2): boolean {
>assign1 = function(p1, p2): boolean {    return true;} : (p1: any, p2: any) => boolean
>assign1 : (p1: any, p2: any) => p1 is A
>function(p1, p2): boolean {    return true;} : (p1: any, p2: any) => boolean
>p1 : any
>p2 : any

    return true;
>true : true

};

// Must have matching parameter index
var assign2: (p1, p2) => p1 is A;
>assign2 : (p1: any, p2: any) => p1 is A
>p1 : any
>p2 : any

assign2 = function(p1, p2): p2 is A {
>assign2 = function(p1, p2): p2 is A {    return true;} : (p1: any, p2: any) => p2 is A
>assign2 : (p1: any, p2: any) => p1 is A
>function(p1, p2): p2 is A {    return true;} : (p1: any, p2: any) => p2 is A
>p1 : any
>p2 : any

    return true;
>true : true

};

// No matching signature
var assign3: (p1, p2) => p1 is A;
>assign3 : (p1: any, p2: any) => p1 is A
>p1 : any
>p2 : any

assign3 = function(p1, p2, p3): p1 is A {
>assign3 = function(p1, p2, p3): p1 is A {    return true;} : (p1: any, p2: any, p3: any) => p1 is A
>assign3 : (p1: any, p2: any) => p1 is A
>function(p1, p2, p3): p1 is A {    return true;} : (p1: any, p2: any, p3: any) => p1 is A
>p1 : any
>p2 : any
>p3 : any

    return true;
>true : true

};

// Type predicates in non-return type positions
var b1: b is A;
>b1 : any
>is : any
>A : any

function b2(a: b is A) {};
>b2 : (a: any, is: any, A: any) => void
>a : any
>is : any
>A : any

function b3(): A | b is A {
>b3 : () => any
>is : any
>A : typeof A

    return true;
>true : true

};

// Non-compatiable type predicate positions for signature declarations
class D {
>D : D

    constructor(p1: A): p1 is C {
>p1 : A

        return true;
>true : true
    }
    get m1(p1: A): p1 is C {
>m1 : boolean
>p1 : A

        return true;
>true : true
    }
    set m2(p1: A): p1 is C {
>m2 : A
>p1 : A

        return true;
>true : true
    }
}

interface I1 {
    new (p1: A): p1 is C;
>p1 : A
}

interface I2 {
    [index: number]: p1 is C;
>index : number
>is : any
>C : typeof C
}

// Reference to rest parameter
function b4(...a): a is A {
>b4 : (...a: any[]) => a is A
>a : any[]

    return true;
>true : true
}

// Reference to binding pattern
function b5({a, b, p1}, p2, p3): p1 is A {
>b5 : ({ a, b, p1 }: { a: any; b: any; p1: any; }, p2: any, p3: any) => p1 is A
>a : any
>b : any
>p1 : any
>p2 : any
>p3 : any

    return true;
>true : true
}

function b6([a, b, p1], p2, p3): p1 is A {
>b6 : ([a, b, p1]: [any, any, any], p2: any, p3: any) => p1 is A
>a : any
>b : any
>p1 : any
>p2 : any
>p3 : any

    return true;
>true : true
}

function b7({a, b, c: {p1}}, p2, p3): p1 is A {
>b7 : ({ a, b, c: { p1 } }: { a: any; b: any; c: { p1: any; }; }, p2: any, p3: any) => p1 is A
>a : any
>b : any
>c : any
>p1 : any
>p2 : any
>p3 : any

    return true;
>true : true
}

// Should not crash the compiler
var x: A;
>x : A

if (hasMissingParameter()) {
>hasMissingParameter() : boolean
>hasMissingParameter : () => x is A

    x.propA;
>x.propA : number
>x : A
>propA : number
}

// repro #17297

type Keys = 'a'|'b'|'c'
>Keys : Keys

type KeySet<T extends Keys> = { [k in T]: true }
>KeySet : KeySet<T>
>true : true

// expected an error, since Keys doesn't have a 'd'
declare function hasKey<T extends Keys>(x: KeySet<T>): x is KeySet<T|'d'>;
>hasKey : <T extends Keys>(x: KeySet<T>) => x is KeySet<T | "d">
>x : KeySet<T>

type Foo = { 'a': string; }
>Foo : Foo
>'a' : string

type Bar = { 'a': number; }
>Bar : Bar
>'a' : number

interface NeedsFoo<T extends Foo> {
    foo: T;
>foo : T

    isFoo(): this is NeedsFoo<Bar>; // should error
>isFoo : () => this is NeedsFoo<Bar>

};

declare var anError: NeedsFoo<Bar>; // error, as expected
>anError : NeedsFoo<Bar>

declare var alsoAnError: NeedsFoo<number>; // also error, as expected
>alsoAnError : NeedsFoo<number>

declare function newError1(x: any): x is NeedsFoo<Bar>; // should error
>newError1 : (x: any) => x is NeedsFoo<Bar>
>x : any

declare function newError2(x: any): x is NeedsFoo<number>; // should error
>newError2 : (x: any) => x is NeedsFoo<number>
>x : any

declare function newError3(x: number): x is NeedsFoo<number>; // should error
>newError3 : (x: number) => x is NeedsFoo<number>
>x : number