338 lines
18 KiB
Text
338 lines
18 KiB
Text
=== tests/cases/conformance/expressions/typeGuards/typeGuardsInFunction.ts ===
|
|
// Note that type guards affect types of variables and parameters only and
|
|
// have no effect on members of objects such as properties.
|
|
|
|
// variables in global
|
|
var num: number;
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
|
|
var var1: string | number;
|
|
>var1 : string | number, Symbol(var1,Decl(typeGuardsInFunction.ts,5,3))
|
|
|
|
// Inside function declaration
|
|
function f(param: string | number) {
|
|
>f : (param: string | number) => void, Symbol(f,Decl(typeGuardsInFunction.ts,5,26))
|
|
>param : string | number, Symbol(param,Decl(typeGuardsInFunction.ts,7,11))
|
|
|
|
// global vars in function declaration
|
|
num = typeof var1 === "string" && var1.length; // string
|
|
>num = typeof var1 === "string" && var1.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof var1 === "string" && var1.length : number
|
|
>typeof var1 === "string" : boolean
|
|
>typeof var1 : string
|
|
>var1 : string | number, Symbol(var1,Decl(typeGuardsInFunction.ts,5,3))
|
|
>"string" : string
|
|
>var1.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>var1 : string, Symbol(var1,Decl(typeGuardsInFunction.ts,5,3))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
// variables in function declaration
|
|
var var2: string | number;
|
|
>var2 : string | number, Symbol(var2,Decl(typeGuardsInFunction.ts,12,7))
|
|
|
|
num = typeof var2 === "string" && var2.length; // string
|
|
>num = typeof var2 === "string" && var2.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof var2 === "string" && var2.length : number
|
|
>typeof var2 === "string" : boolean
|
|
>typeof var2 : string
|
|
>var2 : string | number, Symbol(var2,Decl(typeGuardsInFunction.ts,12,7))
|
|
>"string" : string
|
|
>var2.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>var2 : string, Symbol(var2,Decl(typeGuardsInFunction.ts,12,7))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
// parameters in function declaration
|
|
num = typeof param === "string" && param.length; // string
|
|
>num = typeof param === "string" && param.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof param === "string" && param.length : number
|
|
>typeof param === "string" : boolean
|
|
>typeof param : string
|
|
>param : string | number, Symbol(param,Decl(typeGuardsInFunction.ts,7,11))
|
|
>"string" : string
|
|
>param.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>param : string, Symbol(param,Decl(typeGuardsInFunction.ts,7,11))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
}
|
|
// local function declaration
|
|
function f1(param: string | number) {
|
|
>f1 : (param: string | number) => void, Symbol(f1,Decl(typeGuardsInFunction.ts,17,1))
|
|
>param : string | number, Symbol(param,Decl(typeGuardsInFunction.ts,19,12))
|
|
|
|
var var2: string | number;
|
|
>var2 : string | number, Symbol(var2,Decl(typeGuardsInFunction.ts,20,7))
|
|
|
|
function f2(param1: string | number) {
|
|
>f2 : (param1: string | number) => void, Symbol(f2,Decl(typeGuardsInFunction.ts,20,30))
|
|
>param1 : string | number, Symbol(param1,Decl(typeGuardsInFunction.ts,21,16))
|
|
|
|
// global vars in function declaration
|
|
num = typeof var1 === "string" && var1.length; // string
|
|
>num = typeof var1 === "string" && var1.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof var1 === "string" && var1.length : number
|
|
>typeof var1 === "string" : boolean
|
|
>typeof var1 : string
|
|
>var1 : string | number, Symbol(var1,Decl(typeGuardsInFunction.ts,5,3))
|
|
>"string" : string
|
|
>var1.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>var1 : string, Symbol(var1,Decl(typeGuardsInFunction.ts,5,3))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
// variables from outer function declaration
|
|
num = typeof var2 === "string" && var2.length; // string
|
|
>num = typeof var2 === "string" && var2.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof var2 === "string" && var2.length : number
|
|
>typeof var2 === "string" : boolean
|
|
>typeof var2 : string
|
|
>var2 : string | number, Symbol(var2,Decl(typeGuardsInFunction.ts,20,7))
|
|
>"string" : string
|
|
>var2.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>var2 : string, Symbol(var2,Decl(typeGuardsInFunction.ts,20,7))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
// parameters in outer declaration
|
|
num = typeof param === "string" && param.length; // string
|
|
>num = typeof param === "string" && param.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof param === "string" && param.length : number
|
|
>typeof param === "string" : boolean
|
|
>typeof param : string
|
|
>param : string | number, Symbol(param,Decl(typeGuardsInFunction.ts,19,12))
|
|
>"string" : string
|
|
>param.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>param : string, Symbol(param,Decl(typeGuardsInFunction.ts,19,12))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
// local
|
|
var var3: string | number;
|
|
>var3 : string | number, Symbol(var3,Decl(typeGuardsInFunction.ts,32,11))
|
|
|
|
num = typeof var3 === "string" && var3.length; // string
|
|
>num = typeof var3 === "string" && var3.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof var3 === "string" && var3.length : number
|
|
>typeof var3 === "string" : boolean
|
|
>typeof var3 : string
|
|
>var3 : string | number, Symbol(var3,Decl(typeGuardsInFunction.ts,32,11))
|
|
>"string" : string
|
|
>var3.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>var3 : string, Symbol(var3,Decl(typeGuardsInFunction.ts,32,11))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
num = typeof param1 === "string" && param1.length; // string
|
|
>num = typeof param1 === "string" && param1.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof param1 === "string" && param1.length : number
|
|
>typeof param1 === "string" : boolean
|
|
>typeof param1 : string
|
|
>param1 : string | number, Symbol(param1,Decl(typeGuardsInFunction.ts,21,16))
|
|
>"string" : string
|
|
>param1.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>param1 : string, Symbol(param1,Decl(typeGuardsInFunction.ts,21,16))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
}
|
|
}
|
|
// Function expression
|
|
function f2(param: string | number) {
|
|
>f2 : (param: string | number) => void, Symbol(f2,Decl(typeGuardsInFunction.ts,36,1))
|
|
>param : string | number, Symbol(param,Decl(typeGuardsInFunction.ts,38,12))
|
|
|
|
// variables in function declaration
|
|
var var2: string | number;
|
|
>var2 : string | number, Symbol(var2,Decl(typeGuardsInFunction.ts,40,7))
|
|
|
|
// variables in function expressions
|
|
var r = function (param1: string | number) {
|
|
>r : void, Symbol(r,Decl(typeGuardsInFunction.ts,42,7))
|
|
>function (param1: string | number) { // global vars in function declaration num = typeof var1 === "string" && var1.length; // string // variables from outer function declaration num = typeof var2 === "string" && var2.length; // string // parameters in outer declaration num = typeof param === "string" && param.length; // string // local var var3: string | number; num = typeof var3 === "string" && var3.length; // string num = typeof param1 === "string" && param1.length; // string } (param) : void
|
|
>function (param1: string | number) { // global vars in function declaration num = typeof var1 === "string" && var1.length; // string // variables from outer function declaration num = typeof var2 === "string" && var2.length; // string // parameters in outer declaration num = typeof param === "string" && param.length; // string // local var var3: string | number; num = typeof var3 === "string" && var3.length; // string num = typeof param1 === "string" && param1.length; // string } : (param1: string | number) => void
|
|
>param1 : string | number, Symbol(param1,Decl(typeGuardsInFunction.ts,42,22))
|
|
|
|
// global vars in function declaration
|
|
num = typeof var1 === "string" && var1.length; // string
|
|
>num = typeof var1 === "string" && var1.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof var1 === "string" && var1.length : number
|
|
>typeof var1 === "string" : boolean
|
|
>typeof var1 : string
|
|
>var1 : string | number, Symbol(var1,Decl(typeGuardsInFunction.ts,5,3))
|
|
>"string" : string
|
|
>var1.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>var1 : string, Symbol(var1,Decl(typeGuardsInFunction.ts,5,3))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
// variables from outer function declaration
|
|
num = typeof var2 === "string" && var2.length; // string
|
|
>num = typeof var2 === "string" && var2.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof var2 === "string" && var2.length : number
|
|
>typeof var2 === "string" : boolean
|
|
>typeof var2 : string
|
|
>var2 : string | number, Symbol(var2,Decl(typeGuardsInFunction.ts,40,7))
|
|
>"string" : string
|
|
>var2.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>var2 : string, Symbol(var2,Decl(typeGuardsInFunction.ts,40,7))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
// parameters in outer declaration
|
|
num = typeof param === "string" && param.length; // string
|
|
>num = typeof param === "string" && param.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof param === "string" && param.length : number
|
|
>typeof param === "string" : boolean
|
|
>typeof param : string
|
|
>param : string | number, Symbol(param,Decl(typeGuardsInFunction.ts,38,12))
|
|
>"string" : string
|
|
>param.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>param : string, Symbol(param,Decl(typeGuardsInFunction.ts,38,12))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
// local
|
|
var var3: string | number;
|
|
>var3 : string | number, Symbol(var3,Decl(typeGuardsInFunction.ts,53,11))
|
|
|
|
num = typeof var3 === "string" && var3.length; // string
|
|
>num = typeof var3 === "string" && var3.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof var3 === "string" && var3.length : number
|
|
>typeof var3 === "string" : boolean
|
|
>typeof var3 : string
|
|
>var3 : string | number, Symbol(var3,Decl(typeGuardsInFunction.ts,53,11))
|
|
>"string" : string
|
|
>var3.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>var3 : string, Symbol(var3,Decl(typeGuardsInFunction.ts,53,11))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
num = typeof param1 === "string" && param1.length; // string
|
|
>num = typeof param1 === "string" && param1.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof param1 === "string" && param1.length : number
|
|
>typeof param1 === "string" : boolean
|
|
>typeof param1 : string
|
|
>param1 : string | number, Symbol(param1,Decl(typeGuardsInFunction.ts,42,22))
|
|
>"string" : string
|
|
>param1.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>param1 : string, Symbol(param1,Decl(typeGuardsInFunction.ts,42,22))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
} (param);
|
|
>param : string | number, Symbol(param,Decl(typeGuardsInFunction.ts,38,12))
|
|
}
|
|
// Arrow expression
|
|
function f3(param: string | number) {
|
|
>f3 : (param: string | number) => void, Symbol(f3,Decl(typeGuardsInFunction.ts,57,1))
|
|
>param : string | number, Symbol(param,Decl(typeGuardsInFunction.ts,59,12))
|
|
|
|
// variables in function declaration
|
|
var var2: string | number;
|
|
>var2 : string | number, Symbol(var2,Decl(typeGuardsInFunction.ts,61,7))
|
|
|
|
// variables in function expressions
|
|
var r = ((param1: string | number) => {
|
|
>r : void, Symbol(r,Decl(typeGuardsInFunction.ts,63,7))
|
|
>((param1: string | number) => { // global vars in function declaration num = typeof var1 === "string" && var1.length; // string // variables from outer function declaration num = typeof var2 === "string" && var2.length; // string // parameters in outer declaration num = typeof param === "string" && param.length; // string // local var var3: string | number; num = typeof var3 === "string" && var3.length; // string num = typeof param1 === "string" && param1.length; // string })(param) : void
|
|
>((param1: string | number) => { // global vars in function declaration num = typeof var1 === "string" && var1.length; // string // variables from outer function declaration num = typeof var2 === "string" && var2.length; // string // parameters in outer declaration num = typeof param === "string" && param.length; // string // local var var3: string | number; num = typeof var3 === "string" && var3.length; // string num = typeof param1 === "string" && param1.length; // string }) : (param1: string | number) => void
|
|
>(param1: string | number) => { // global vars in function declaration num = typeof var1 === "string" && var1.length; // string // variables from outer function declaration num = typeof var2 === "string" && var2.length; // string // parameters in outer declaration num = typeof param === "string" && param.length; // string // local var var3: string | number; num = typeof var3 === "string" && var3.length; // string num = typeof param1 === "string" && param1.length; // string } : (param1: string | number) => void
|
|
>param1 : string | number, Symbol(param1,Decl(typeGuardsInFunction.ts,63,14))
|
|
|
|
// global vars in function declaration
|
|
num = typeof var1 === "string" && var1.length; // string
|
|
>num = typeof var1 === "string" && var1.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof var1 === "string" && var1.length : number
|
|
>typeof var1 === "string" : boolean
|
|
>typeof var1 : string
|
|
>var1 : string | number, Symbol(var1,Decl(typeGuardsInFunction.ts,5,3))
|
|
>"string" : string
|
|
>var1.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>var1 : string, Symbol(var1,Decl(typeGuardsInFunction.ts,5,3))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
// variables from outer function declaration
|
|
num = typeof var2 === "string" && var2.length; // string
|
|
>num = typeof var2 === "string" && var2.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof var2 === "string" && var2.length : number
|
|
>typeof var2 === "string" : boolean
|
|
>typeof var2 : string
|
|
>var2 : string | number, Symbol(var2,Decl(typeGuardsInFunction.ts,61,7))
|
|
>"string" : string
|
|
>var2.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>var2 : string, Symbol(var2,Decl(typeGuardsInFunction.ts,61,7))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
// parameters in outer declaration
|
|
num = typeof param === "string" && param.length; // string
|
|
>num = typeof param === "string" && param.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof param === "string" && param.length : number
|
|
>typeof param === "string" : boolean
|
|
>typeof param : string
|
|
>param : string | number, Symbol(param,Decl(typeGuardsInFunction.ts,59,12))
|
|
>"string" : string
|
|
>param.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>param : string, Symbol(param,Decl(typeGuardsInFunction.ts,59,12))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
// local
|
|
var var3: string | number;
|
|
>var3 : string | number, Symbol(var3,Decl(typeGuardsInFunction.ts,74,11))
|
|
|
|
num = typeof var3 === "string" && var3.length; // string
|
|
>num = typeof var3 === "string" && var3.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof var3 === "string" && var3.length : number
|
|
>typeof var3 === "string" : boolean
|
|
>typeof var3 : string
|
|
>var3 : string | number, Symbol(var3,Decl(typeGuardsInFunction.ts,74,11))
|
|
>"string" : string
|
|
>var3.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>var3 : string, Symbol(var3,Decl(typeGuardsInFunction.ts,74,11))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
num = typeof param1 === "string" && param1.length; // string
|
|
>num = typeof param1 === "string" && param1.length : number
|
|
>num : number, Symbol(num,Decl(typeGuardsInFunction.ts,4,3))
|
|
>typeof param1 === "string" && param1.length : number
|
|
>typeof param1 === "string" : boolean
|
|
>typeof param1 : string
|
|
>param1 : string | number, Symbol(param1,Decl(typeGuardsInFunction.ts,63,14))
|
|
>"string" : string
|
|
>param1.length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
>param1 : string, Symbol(param1,Decl(typeGuardsInFunction.ts,63,14))
|
|
>length : number, Symbol(String.length,Decl(lib.d.ts,414,19))
|
|
|
|
})(param);
|
|
>param : string | number, Symbol(param,Decl(typeGuardsInFunction.ts,59,12))
|
|
}
|
|
// Return type of function
|
|
// Inside function declaration
|
|
var strOrNum: string | number;
|
|
>strOrNum : string | number, Symbol(strOrNum,Decl(typeGuardsInFunction.ts,81,3))
|
|
|
|
function f4() {
|
|
>f4 : () => string | number, Symbol(f4,Decl(typeGuardsInFunction.ts,81,30))
|
|
|
|
var var2: string | number = strOrNum;
|
|
>var2 : string | number, Symbol(var2,Decl(typeGuardsInFunction.ts,83,7))
|
|
>strOrNum : string | number, Symbol(strOrNum,Decl(typeGuardsInFunction.ts,81,3))
|
|
|
|
return var2;
|
|
>var2 : string | number, Symbol(var2,Decl(typeGuardsInFunction.ts,83,7))
|
|
}
|
|
strOrNum = typeof f4() === "string" && f4(); // string | number
|
|
>strOrNum = typeof f4() === "string" && f4() : string | number
|
|
>strOrNum : string | number, Symbol(strOrNum,Decl(typeGuardsInFunction.ts,81,3))
|
|
>typeof f4() === "string" && f4() : string | number
|
|
>typeof f4() === "string" : boolean
|
|
>typeof f4() : string
|
|
>f4() : string | number
|
|
>f4 : () => string | number, Symbol(f4,Decl(typeGuardsInFunction.ts,81,30))
|
|
>"string" : string
|
|
>f4() : string | number
|
|
>f4 : () => string | number, Symbol(f4,Decl(typeGuardsInFunction.ts,81,30))
|
|
|