From spec: Note that type guards affect types of variables and parameters only and have no effect on members of objects such as properties
319 lines
12 KiB
Text
319 lines
12 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
|
|
|
|
var var1: string | number;
|
|
>var1 : string | number
|
|
|
|
// Inside function declaration
|
|
function f(param: string | number) {
|
|
>f : (param: string | number) => void
|
|
>param : string | number
|
|
|
|
// global vars in function declaration
|
|
num = typeof var1 === "string" && var1.length; // string
|
|
>num = typeof var1 === "string" && var1.length : number
|
|
>num : number
|
|
>typeof var1 === "string" && var1.length : number
|
|
>typeof var1 === "string" : boolean
|
|
>typeof var1 : string
|
|
>var1 : string | number
|
|
>var1.length : number
|
|
>var1 : string
|
|
>length : number
|
|
|
|
// variables in function declaration
|
|
var var2: string | number;
|
|
>var2 : string | number
|
|
|
|
num = typeof var2 === "string" && var2.length; // string
|
|
>num = typeof var2 === "string" && var2.length : number
|
|
>num : number
|
|
>typeof var2 === "string" && var2.length : number
|
|
>typeof var2 === "string" : boolean
|
|
>typeof var2 : string
|
|
>var2 : string | number
|
|
>var2.length : number
|
|
>var2 : string
|
|
>length : number
|
|
|
|
// parameters in function declaration
|
|
num = typeof param === "string" && param.length; // string
|
|
>num = typeof param === "string" && param.length : number
|
|
>num : number
|
|
>typeof param === "string" && param.length : number
|
|
>typeof param === "string" : boolean
|
|
>typeof param : string
|
|
>param : string | number
|
|
>param.length : number
|
|
>param : string
|
|
>length : number
|
|
}
|
|
// local function declaration
|
|
function f1(param: string | number) {
|
|
>f1 : (param: string | number) => void
|
|
>param : string | number
|
|
|
|
var var2: string | number;
|
|
>var2 : string | number
|
|
|
|
function f2(param1: string | number) {
|
|
>f2 : (param1: string | number) => void
|
|
>param1 : string | number
|
|
|
|
// global vars in function declaration
|
|
num = typeof var1 === "string" && var1.length; // string
|
|
>num = typeof var1 === "string" && var1.length : number
|
|
>num : number
|
|
>typeof var1 === "string" && var1.length : number
|
|
>typeof var1 === "string" : boolean
|
|
>typeof var1 : string
|
|
>var1 : string | number
|
|
>var1.length : number
|
|
>var1 : string
|
|
>length : number
|
|
|
|
// variables from outer function declaration
|
|
num = typeof var2 === "string" && var2.length; // string
|
|
>num = typeof var2 === "string" && var2.length : number
|
|
>num : number
|
|
>typeof var2 === "string" && var2.length : number
|
|
>typeof var2 === "string" : boolean
|
|
>typeof var2 : string
|
|
>var2 : string | number
|
|
>var2.length : number
|
|
>var2 : string
|
|
>length : number
|
|
|
|
// parameters in outer declaration
|
|
num = typeof param === "string" && param.length; // string
|
|
>num = typeof param === "string" && param.length : number
|
|
>num : number
|
|
>typeof param === "string" && param.length : number
|
|
>typeof param === "string" : boolean
|
|
>typeof param : string
|
|
>param : string | number
|
|
>param.length : number
|
|
>param : string
|
|
>length : number
|
|
|
|
// local
|
|
var var3: string | number;
|
|
>var3 : string | number
|
|
|
|
num = typeof var3 === "string" && var3.length; // string
|
|
>num = typeof var3 === "string" && var3.length : number
|
|
>num : number
|
|
>typeof var3 === "string" && var3.length : number
|
|
>typeof var3 === "string" : boolean
|
|
>typeof var3 : string
|
|
>var3 : string | number
|
|
>var3.length : number
|
|
>var3 : string
|
|
>length : number
|
|
|
|
num = typeof param1 === "string" && param1.length; // string
|
|
>num = typeof param1 === "string" && param1.length : number
|
|
>num : number
|
|
>typeof param1 === "string" && param1.length : number
|
|
>typeof param1 === "string" : boolean
|
|
>typeof param1 : string
|
|
>param1 : string | number
|
|
>param1.length : number
|
|
>param1 : string
|
|
>length : number
|
|
}
|
|
}
|
|
// Function expression
|
|
function f2(param: string | number) {
|
|
>f2 : (param: string | number) => void
|
|
>param : string | number
|
|
|
|
// variables in function declaration
|
|
var var2: string | number;
|
|
>var2 : string | number
|
|
|
|
// variables in function expressions
|
|
var r = function (param1: string | number) {
|
|
>r : 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 } (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
|
|
|
|
// global vars in function declaration
|
|
num = typeof var1 === "string" && var1.length; // string
|
|
>num = typeof var1 === "string" && var1.length : number
|
|
>num : number
|
|
>typeof var1 === "string" && var1.length : number
|
|
>typeof var1 === "string" : boolean
|
|
>typeof var1 : string
|
|
>var1 : string | number
|
|
>var1.length : number
|
|
>var1 : string
|
|
>length : number
|
|
|
|
// variables from outer function declaration
|
|
num = typeof var2 === "string" && var2.length; // string
|
|
>num = typeof var2 === "string" && var2.length : number
|
|
>num : number
|
|
>typeof var2 === "string" && var2.length : number
|
|
>typeof var2 === "string" : boolean
|
|
>typeof var2 : string
|
|
>var2 : string | number
|
|
>var2.length : number
|
|
>var2 : string
|
|
>length : number
|
|
|
|
// parameters in outer declaration
|
|
num = typeof param === "string" && param.length; // string
|
|
>num = typeof param === "string" && param.length : number
|
|
>num : number
|
|
>typeof param === "string" && param.length : number
|
|
>typeof param === "string" : boolean
|
|
>typeof param : string
|
|
>param : string | number
|
|
>param.length : number
|
|
>param : string
|
|
>length : number
|
|
|
|
// local
|
|
var var3: string | number;
|
|
>var3 : string | number
|
|
|
|
num = typeof var3 === "string" && var3.length; // string
|
|
>num = typeof var3 === "string" && var3.length : number
|
|
>num : number
|
|
>typeof var3 === "string" && var3.length : number
|
|
>typeof var3 === "string" : boolean
|
|
>typeof var3 : string
|
|
>var3 : string | number
|
|
>var3.length : number
|
|
>var3 : string
|
|
>length : number
|
|
|
|
num = typeof param1 === "string" && param1.length; // string
|
|
>num = typeof param1 === "string" && param1.length : number
|
|
>num : number
|
|
>typeof param1 === "string" && param1.length : number
|
|
>typeof param1 === "string" : boolean
|
|
>typeof param1 : string
|
|
>param1 : string | number
|
|
>param1.length : number
|
|
>param1 : string
|
|
>length : number
|
|
|
|
} (param);
|
|
>param : string | number
|
|
}
|
|
// Arrow expression
|
|
function f3(param: string | number) {
|
|
>f3 : (param: string | number) => void
|
|
>param : string | number
|
|
|
|
// variables in function declaration
|
|
var var2: string | number;
|
|
>var2 : string | number
|
|
|
|
// variables in function expressions
|
|
var r = ((param1: string | number) => {
|
|
>r : 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 })(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
|
|
|
|
// global vars in function declaration
|
|
num = typeof var1 === "string" && var1.length; // string
|
|
>num = typeof var1 === "string" && var1.length : number
|
|
>num : number
|
|
>typeof var1 === "string" && var1.length : number
|
|
>typeof var1 === "string" : boolean
|
|
>typeof var1 : string
|
|
>var1 : string | number
|
|
>var1.length : number
|
|
>var1 : string
|
|
>length : number
|
|
|
|
// variables from outer function declaration
|
|
num = typeof var2 === "string" && var2.length; // string
|
|
>num = typeof var2 === "string" && var2.length : number
|
|
>num : number
|
|
>typeof var2 === "string" && var2.length : number
|
|
>typeof var2 === "string" : boolean
|
|
>typeof var2 : string
|
|
>var2 : string | number
|
|
>var2.length : number
|
|
>var2 : string
|
|
>length : number
|
|
|
|
// parameters in outer declaration
|
|
num = typeof param === "string" && param.length; // string
|
|
>num = typeof param === "string" && param.length : number
|
|
>num : number
|
|
>typeof param === "string" && param.length : number
|
|
>typeof param === "string" : boolean
|
|
>typeof param : string
|
|
>param : string | number
|
|
>param.length : number
|
|
>param : string
|
|
>length : number
|
|
|
|
// local
|
|
var var3: string | number;
|
|
>var3 : string | number
|
|
|
|
num = typeof var3 === "string" && var3.length; // string
|
|
>num = typeof var3 === "string" && var3.length : number
|
|
>num : number
|
|
>typeof var3 === "string" && var3.length : number
|
|
>typeof var3 === "string" : boolean
|
|
>typeof var3 : string
|
|
>var3 : string | number
|
|
>var3.length : number
|
|
>var3 : string
|
|
>length : number
|
|
|
|
num = typeof param1 === "string" && param1.length; // string
|
|
>num = typeof param1 === "string" && param1.length : number
|
|
>num : number
|
|
>typeof param1 === "string" && param1.length : number
|
|
>typeof param1 === "string" : boolean
|
|
>typeof param1 : string
|
|
>param1 : string | number
|
|
>param1.length : number
|
|
>param1 : string
|
|
>length : number
|
|
|
|
})(param);
|
|
>param : string | number
|
|
}
|
|
// Return type of function
|
|
// Inside function declaration
|
|
var strOrNum: string | number;
|
|
>strOrNum : string | number
|
|
|
|
function f4() {
|
|
>f4 : () => string | number
|
|
|
|
var var2: string | number = strOrNum;
|
|
>var2 : string | number
|
|
>strOrNum : string | number
|
|
|
|
return var2;
|
|
>var2 : string | number
|
|
}
|
|
strOrNum = typeof f4() === "string" && f4(); // string | number
|
|
>strOrNum = typeof f4() === "string" && f4() : string | number
|
|
>strOrNum : string | number
|
|
>typeof f4() === "string" && f4() : string | number
|
|
>typeof f4() === "string" : boolean
|
|
>typeof f4() : string
|
|
>f4() : string | number
|
|
>f4 : () => string | number
|
|
>f4() : string | number
|
|
>f4 : () => string | number
|
|
|