187 lines
7 KiB
Text
187 lines
7 KiB
Text
=== tests/cases/conformance/expressions/typeGuards/typeGuardsObjectMethods.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 strOrNum: string | number;
|
|
>strOrNum : string | number
|
|
|
|
var var1: string | number;
|
|
>var1 : string | number
|
|
|
|
var obj1 = {
|
|
>obj1 : { method(param: string | number): string | number; prop: string | number; }
|
|
>{ // Inside method method(param: string | number) { // global vars in function declaration num = typeof var1 === "string" && var1.length; // string // variables in function declaration var var2: string | number; num = typeof var2 === "string" && var2.length; // string // parameters in function declaration num = typeof param === "string" && param.length; // string return strOrNum; }, get prop() { // global vars in function declaration num = typeof var1 === "string" && var1.length; // string // variables in function declaration var var2: string | number; num = typeof var2 === "string" && var2.length; // string return strOrNum; }, set prop(param: string | number) { // global vars in function declaration num = typeof var1 === "string" && var1.length; // string // variables in function declaration var var2: string | number; num = typeof var2 === "string" && var2.length; // string // parameters in function declaration num = typeof param === "string" && param.length; // string }} : { method(param: string | number): string | number; prop: string | number; }
|
|
|
|
// Inside method
|
|
method(param: string | number) {
|
|
>method : (param: string | number) => string | number
|
|
>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
|
|
>"string" : string
|
|
>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
|
|
>"string" : string
|
|
>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
|
|
>"string" : string
|
|
>param.length : number
|
|
>param : string
|
|
>length : number
|
|
|
|
return strOrNum;
|
|
>strOrNum : string | number
|
|
|
|
},
|
|
get prop() {
|
|
>prop : 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
|
|
>"string" : string
|
|
>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
|
|
>"string" : string
|
|
>var2.length : number
|
|
>var2 : string
|
|
>length : number
|
|
|
|
return strOrNum;
|
|
>strOrNum : string | number
|
|
|
|
},
|
|
set prop(param: string | number) {
|
|
>prop : string | number
|
|
>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
|
|
>"string" : string
|
|
>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
|
|
>"string" : string
|
|
>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
|
|
>"string" : string
|
|
>param.length : number
|
|
>param : string
|
|
>length : number
|
|
}
|
|
};
|
|
// return expression of the method
|
|
strOrNum = typeof obj1.method(strOrNum) === "string" && obj1.method(strOrNum);
|
|
>strOrNum = typeof obj1.method(strOrNum) === "string" && obj1.method(strOrNum) : string | number
|
|
>strOrNum : string | number
|
|
>typeof obj1.method(strOrNum) === "string" && obj1.method(strOrNum) : string | number
|
|
>typeof obj1.method(strOrNum) === "string" : boolean
|
|
>typeof obj1.method(strOrNum) : string
|
|
>obj1.method(strOrNum) : string | number
|
|
>obj1.method : (param: string | number) => string | number
|
|
>obj1 : { method(param: string | number): string | number; prop: string | number; }
|
|
>method : (param: string | number) => string | number
|
|
>strOrNum : string | number
|
|
>"string" : string
|
|
>obj1.method(strOrNum) : string | number
|
|
>obj1.method : (param: string | number) => string | number
|
|
>obj1 : { method(param: string | number): string | number; prop: string | number; }
|
|
>method : (param: string | number) => string | number
|
|
>strOrNum : string | number
|
|
|
|
// accessing getter property
|
|
strOrNum = typeof obj1.prop === "string" && obj1.prop;
|
|
>strOrNum = typeof obj1.prop === "string" && obj1.prop : string | number
|
|
>strOrNum : string | number
|
|
>typeof obj1.prop === "string" && obj1.prop : string | number
|
|
>typeof obj1.prop === "string" : boolean
|
|
>typeof obj1.prop : string
|
|
>obj1.prop : string | number
|
|
>obj1 : { method(param: string | number): string | number; prop: string | number; }
|
|
>prop : string | number
|
|
>"string" : string
|
|
>obj1.prop : string | number
|
|
>obj1 : { method(param: string | number): string | number; prop: string | number; }
|
|
>prop : string | number
|
|
|