TypeScript/tests/baselines/reference/typeGuardFunction.types
2015-06-07 22:16:09 +08:00

232 lines
3.8 KiB
Plaintext

=== tests/cases/conformance/expressions/typeGuards/typeGuardFunction.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
}
declare function isA(p1: any): p1 is A;
>isA : (p1: any) => boolean
>p1 : any
>p1 : any
>A : A
declare function isB(p1: any): p1 is B;
>isB : (p1: any) => boolean
>p1 : any
>p1 : any
>B : B
declare function isC(p1: any): p1 is C;
>isC : (p1: any) => boolean
>p1 : any
>p1 : any
>C : C
declare function retC(): C;
>retC : () => C
>C : C
var a: A;
>a : A
>A : A
var b: B;
>b : B
>B : B
// Basic
if (isC(a)) {
>isC(a) : boolean
>isC : (p1: any) => boolean
>a : A
a.propC;
>a.propC : number
>a : C
>propC : number
}
// Sub type
var subType: C;
>subType : C
>C : C
if(isA(subType)) {
>isA(subType) : boolean
>isA : (p1: any) => boolean
>subType : C
subType.propC;
>subType.propC : number
>subType : C
>propC : number
}
// Union type
var union: A | B;
>union : A | B
>A : A
>B : B
if(isA(union)) {
>isA(union) : boolean
>isA : (p1: any) => boolean
>union : A | B
union.propA;
>union.propA : number
>union : A
>propA : number
}
// Call signature
interface I1 {
>I1 : I1
(p1: A): p1 is C;
>p1 : A
>A : A
>p1 : any
>C : C
}
// The parameter index and argument index for the type guard target is matching.
// The type predicate type is assignable to the parameter type.
declare function isC_multipleParams(p1, p2): p1 is C;
>isC_multipleParams : (p1: any, p2: any) => boolean
>p1 : any
>p2 : any
>p1 : any
>C : C
if (isC_multipleParams(a, 0)) {
>isC_multipleParams(a, 0) : boolean
>isC_multipleParams : (p1: any, p2: any) => boolean
>a : A
>0 : number
a.propC;
>a.propC : number
>a : C
>propC : number
}
// Methods
var obj: {
>obj : { func1(p1: A): boolean; }
func1(p1: A): p1 is C;
>func1 : (p1: A) => boolean
>p1 : A
>A : A
>p1 : any
>C : C
}
class D {
>D : D
method1(p1: A): p1 is C {
>method1 : (p1: A) => boolean
>p1 : A
>A : A
>p1 : any
>C : C
return true;
>true : boolean
}
}
// Arrow function
let f1 = (p1: A): p1 is C => false;
>f1 : (p1: A) => boolean
>(p1: A): p1 is C => false : (p1: A) => boolean
>p1 : A
>A : A
>p1 : any
>C : C
>false : boolean
// Function type
declare function f2(p1: (p1: A) => p1 is C);
>f2 : (p1: (p1: A) => boolean) => any
>p1 : (p1: A) => boolean
>p1 : A
>A : A
>p1 : any
>C : C
// Function expressions
f2(function(p1: A): p1 is C {
>f2(function(p1: A): p1 is C { return true;}) : any
>f2 : (p1: (p1: A) => boolean) => any
>function(p1: A): p1 is C { return true;} : (p1: A) => boolean
>p1 : A
>A : A
>p1 : any
>C : C
return true;
>true : boolean
});
// Evaluations are asssignable to boolean.
declare function acceptingBoolean(a: boolean);
>acceptingBoolean : (a: boolean) => any
>a : boolean
acceptingBoolean(isA(a));
>acceptingBoolean(isA(a)) : any
>acceptingBoolean : (a: boolean) => any
>isA(a) : boolean
>isA : (p1: any) => boolean
>a : A
// Type predicates with different parameter name.
declare function acceptingTypeGuardFunction(p1: (item) => item is A);
>acceptingTypeGuardFunction : (p1: (item: any) => boolean) => any
>p1 : (item: any) => boolean
>item : any
>item : any
>A : A
acceptingTypeGuardFunction(isA);
>acceptingTypeGuardFunction(isA) : any
>acceptingTypeGuardFunction : (p1: (item: any) => boolean) => any
>isA : (p1: any) => boolean
// Binary expressions
let union2: C | B;
>union2 : B | C
>C : C
>B : B
let union3: boolean | B = isA(union2) || union2;
>union3 : boolean | B
>B : B
>isA(union2) || union2 : boolean | B
>isA(union2) : boolean
>isA : (p1: any) => boolean
>union2 : B | C
>union2 : B