Modifying tests and accepting new baselines
This commit is contained in:
parent
008e36b6ea
commit
91b97009f0
|
@ -17,12 +17,6 @@ var c: C;
|
||||||
// where s is a string literal with the value 'string', 'number', or 'boolean',
|
// where s is a string literal with the value 'string', 'number', or 'boolean',
|
||||||
// - when true, narrows the type of x to the given primitive type, or
|
// - when true, narrows the type of x to the given primitive type, or
|
||||||
// - when false, removes the primitive type from the type of x.
|
// - when false, removes the primitive type from the type of x.
|
||||||
if (typeof strOrNum === "boolean") {
|
|
||||||
bool = strOrNum; // boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var z: string | number = strOrNum; // string | number
|
|
||||||
}
|
|
||||||
if (typeof strOrBool === "boolean") {
|
if (typeof strOrBool === "boolean") {
|
||||||
bool = strOrBool; // boolean
|
bool = strOrBool; // boolean
|
||||||
}
|
}
|
||||||
|
@ -48,15 +42,18 @@ else {
|
||||||
c = boolOrC; // C
|
c = boolOrC; // C
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrNum === "boolean") {
|
||||||
|
var z1: string | number = strOrNum; // string | number
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var z2: string | number = strOrNum; // string | number
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
if (typeof strOrNum !== "boolean") {
|
|
||||||
var z: string | number = strOrNum; // string | number
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
bool = strOrNum; // boolean
|
|
||||||
}
|
|
||||||
if (typeof strOrBool !== "boolean") {
|
if (typeof strOrBool !== "boolean") {
|
||||||
str = strOrBool; // string
|
str = strOrBool; // string
|
||||||
}
|
}
|
||||||
|
@ -80,7 +77,16 @@ if (typeof boolOrC !== "boolean") {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
bool = boolOrC; // boolean
|
bool = boolOrC; // boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrNum !== "boolean") {
|
||||||
|
var z1: string | number = strOrNum; // string | number
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var z2: string | number = strOrNum; // string | number
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//// [typeGuardOfFormTypeOfBoolean.js]
|
//// [typeGuardOfFormTypeOfBoolean.js]
|
||||||
var C = (function () {
|
var C = (function () {
|
||||||
|
@ -104,12 +110,6 @@ var c;
|
||||||
// where s is a string literal with the value 'string', 'number', or 'boolean',
|
// where s is a string literal with the value 'string', 'number', or 'boolean',
|
||||||
// - when true, narrows the type of x to the given primitive type, or
|
// - when true, narrows the type of x to the given primitive type, or
|
||||||
// - when false, removes the primitive type from the type of x.
|
// - when false, removes the primitive type from the type of x.
|
||||||
if (typeof strOrNum === "boolean") {
|
|
||||||
bool = strOrNum; // boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var z = strOrNum; // string | number
|
|
||||||
}
|
|
||||||
if (typeof strOrBool === "boolean") {
|
if (typeof strOrBool === "boolean") {
|
||||||
bool = strOrBool; // boolean
|
bool = strOrBool; // boolean
|
||||||
}
|
}
|
||||||
|
@ -134,15 +134,16 @@ if (typeof boolOrC === "boolean") {
|
||||||
else {
|
else {
|
||||||
c = boolOrC; // C
|
c = boolOrC; // C
|
||||||
}
|
}
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrNum === "boolean") {
|
||||||
|
var z1 = strOrNum; // string | number
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var z2 = strOrNum; // string | number
|
||||||
|
}
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
if (typeof strOrNum !== "boolean") {
|
|
||||||
var z = strOrNum; // string | number
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
bool = strOrNum; // boolean
|
|
||||||
}
|
|
||||||
if (typeof strOrBool !== "boolean") {
|
if (typeof strOrBool !== "boolean") {
|
||||||
str = strOrBool; // string
|
str = strOrBool; // string
|
||||||
}
|
}
|
||||||
|
@ -167,3 +168,10 @@ if (typeof boolOrC !== "boolean") {
|
||||||
else {
|
else {
|
||||||
bool = boolOrC; // boolean
|
bool = boolOrC; // boolean
|
||||||
}
|
}
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrNum !== "boolean") {
|
||||||
|
var z1 = strOrNum; // string | number
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var z2 = strOrNum; // string | number
|
||||||
|
}
|
||||||
|
|
|
@ -44,21 +44,6 @@ var c: C;
|
||||||
// where s is a string literal with the value 'string', 'number', or 'boolean',
|
// where s is a string literal with the value 'string', 'number', or 'boolean',
|
||||||
// - when true, narrows the type of x to the given primitive type, or
|
// - when true, narrows the type of x to the given primitive type, or
|
||||||
// - when false, removes the primitive type from the type of x.
|
// - when false, removes the primitive type from the type of x.
|
||||||
if (typeof strOrNum === "boolean") {
|
|
||||||
>typeof strOrNum === "boolean" : boolean
|
|
||||||
>typeof strOrNum : string
|
|
||||||
>strOrNum : string | number
|
|
||||||
|
|
||||||
bool = strOrNum; // boolean
|
|
||||||
>bool = strOrNum : boolean
|
|
||||||
>bool : boolean
|
|
||||||
>strOrNum : boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var z: string | number = strOrNum; // string | number
|
|
||||||
>z : string | number
|
|
||||||
>strOrNum : string | number
|
|
||||||
}
|
|
||||||
if (typeof strOrBool === "boolean") {
|
if (typeof strOrBool === "boolean") {
|
||||||
>typeof strOrBool === "boolean" : boolean
|
>typeof strOrBool === "boolean" : boolean
|
||||||
>typeof strOrBool : string
|
>typeof strOrBool : string
|
||||||
|
@ -124,24 +109,26 @@ else {
|
||||||
>boolOrC : C
|
>boolOrC : C
|
||||||
}
|
}
|
||||||
|
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
if (typeof strOrNum === "boolean") {
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
>typeof strOrNum === "boolean" : boolean
|
||||||
if (typeof strOrNum !== "boolean") {
|
|
||||||
>typeof strOrNum !== "boolean" : boolean
|
|
||||||
>typeof strOrNum : string
|
>typeof strOrNum : string
|
||||||
>strOrNum : string | number
|
>strOrNum : string | number
|
||||||
|
|
||||||
var z: string | number = strOrNum; // string | number
|
var z1: string | number = strOrNum; // string | number
|
||||||
>z : string | number
|
>z1 : string | number
|
||||||
>strOrNum : string | number
|
>strOrNum : string | number
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
bool = strOrNum; // boolean
|
var z2: string | number = strOrNum; // string | number
|
||||||
>bool = strOrNum : boolean
|
>z2 : string | number
|
||||||
>bool : boolean
|
>strOrNum : string | number
|
||||||
>strOrNum : boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
if (typeof strOrBool !== "boolean") {
|
if (typeof strOrBool !== "boolean") {
|
||||||
>typeof strOrBool !== "boolean" : boolean
|
>typeof strOrBool !== "boolean" : boolean
|
||||||
>typeof strOrBool : string
|
>typeof strOrBool : string
|
||||||
|
@ -206,3 +193,20 @@ else {
|
||||||
>bool : boolean
|
>bool : boolean
|
||||||
>boolOrC : boolean
|
>boolOrC : boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrNum !== "boolean") {
|
||||||
|
>typeof strOrNum !== "boolean" : boolean
|
||||||
|
>typeof strOrNum : string
|
||||||
|
>strOrNum : string | number
|
||||||
|
|
||||||
|
var z1: string | number = strOrNum; // string | number
|
||||||
|
>z1 : string | number
|
||||||
|
>strOrNum : string | number
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var z2: string | number = strOrNum; // string | number
|
||||||
|
>z2 : string | number
|
||||||
|
>strOrNum : string | number
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,12 +23,6 @@ if (typeof strOrNum === "number") {
|
||||||
else {
|
else {
|
||||||
str === strOrNum; // string
|
str === strOrNum; // string
|
||||||
}
|
}
|
||||||
if (typeof strOrBool === "number") {
|
|
||||||
num = strOrBool; // number
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var y: string | boolean = strOrBool; // string | boolean
|
|
||||||
}
|
|
||||||
if (typeof numOrBool === "number") {
|
if (typeof numOrBool === "number") {
|
||||||
num = numOrBool; // number
|
num = numOrBool; // number
|
||||||
}
|
}
|
||||||
|
@ -48,6 +42,14 @@ else {
|
||||||
c = numOrC; // C
|
c = numOrC; // C
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrBool === "number") {
|
||||||
|
var y1: string | boolean = strOrBool; // string | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var y2: string | boolean = strOrBool; // string | boolean
|
||||||
|
}
|
||||||
|
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
|
@ -57,12 +59,6 @@ if (typeof strOrNum !== "number") {
|
||||||
else {
|
else {
|
||||||
num = strOrNum; // number
|
num = strOrNum; // number
|
||||||
}
|
}
|
||||||
if (typeof strOrBool !== "number") {
|
|
||||||
var y: string | boolean = strOrBool; // string | boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
num = strOrBool; // number
|
|
||||||
}
|
|
||||||
if (typeof numOrBool !== "number") {
|
if (typeof numOrBool !== "number") {
|
||||||
var x: number | boolean = numOrBool; // number | boolean
|
var x: number | boolean = numOrBool; // number | boolean
|
||||||
}
|
}
|
||||||
|
@ -80,7 +76,16 @@ if (typeof numOrC !== "number") {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
num = numOrC; // number
|
num = numOrC; // number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrBool !== "number") {
|
||||||
|
var y1: string | boolean = strOrBool; // string | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var y2: string | boolean = strOrBool; // string | boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//// [typeGuardOfFormTypeOfNumber.js]
|
//// [typeGuardOfFormTypeOfNumber.js]
|
||||||
var C = (function () {
|
var C = (function () {
|
||||||
|
@ -110,12 +115,6 @@ if (typeof strOrNum === "number") {
|
||||||
else {
|
else {
|
||||||
str === strOrNum; // string
|
str === strOrNum; // string
|
||||||
}
|
}
|
||||||
if (typeof strOrBool === "number") {
|
|
||||||
num = strOrBool; // number
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var y = strOrBool; // string | boolean
|
|
||||||
}
|
|
||||||
if (typeof numOrBool === "number") {
|
if (typeof numOrBool === "number") {
|
||||||
num = numOrBool; // number
|
num = numOrBool; // number
|
||||||
}
|
}
|
||||||
|
@ -134,6 +133,13 @@ if (typeof numOrC === "number") {
|
||||||
else {
|
else {
|
||||||
c = numOrC; // C
|
c = numOrC; // C
|
||||||
}
|
}
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrBool === "number") {
|
||||||
|
var y1 = strOrBool; // string | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var y2 = strOrBool; // string | boolean
|
||||||
|
}
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
|
@ -143,12 +149,6 @@ if (typeof strOrNum !== "number") {
|
||||||
else {
|
else {
|
||||||
num = strOrNum; // number
|
num = strOrNum; // number
|
||||||
}
|
}
|
||||||
if (typeof strOrBool !== "number") {
|
|
||||||
var y = strOrBool; // string | boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
num = strOrBool; // number
|
|
||||||
}
|
|
||||||
if (typeof numOrBool !== "number") {
|
if (typeof numOrBool !== "number") {
|
||||||
var x = numOrBool; // number | boolean
|
var x = numOrBool; // number | boolean
|
||||||
}
|
}
|
||||||
|
@ -167,3 +167,10 @@ if (typeof numOrC !== "number") {
|
||||||
else {
|
else {
|
||||||
num = numOrC; // number
|
num = numOrC; // number
|
||||||
}
|
}
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrBool !== "number") {
|
||||||
|
var y1 = strOrBool; // string | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var y2 = strOrBool; // string | boolean
|
||||||
|
}
|
||||||
|
|
|
@ -60,21 +60,6 @@ else {
|
||||||
>str : string
|
>str : string
|
||||||
>strOrNum : string
|
>strOrNum : string
|
||||||
}
|
}
|
||||||
if (typeof strOrBool === "number") {
|
|
||||||
>typeof strOrBool === "number" : boolean
|
|
||||||
>typeof strOrBool : string
|
|
||||||
>strOrBool : string | boolean
|
|
||||||
|
|
||||||
num = strOrBool; // number
|
|
||||||
>num = strOrBool : number
|
|
||||||
>num : number
|
|
||||||
>strOrBool : number
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var y: string | boolean = strOrBool; // string | boolean
|
|
||||||
>y : string | boolean
|
|
||||||
>strOrBool : string | boolean
|
|
||||||
}
|
|
||||||
if (typeof numOrBool === "number") {
|
if (typeof numOrBool === "number") {
|
||||||
>typeof numOrBool === "number" : boolean
|
>typeof numOrBool === "number" : boolean
|
||||||
>typeof numOrBool : string
|
>typeof numOrBool : string
|
||||||
|
@ -123,6 +108,22 @@ else {
|
||||||
>numOrC : C
|
>numOrC : C
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrBool === "number") {
|
||||||
|
>typeof strOrBool === "number" : boolean
|
||||||
|
>typeof strOrBool : string
|
||||||
|
>strOrBool : string | boolean
|
||||||
|
|
||||||
|
var y1: string | boolean = strOrBool; // string | boolean
|
||||||
|
>y1 : string | boolean
|
||||||
|
>strOrBool : string | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var y2: string | boolean = strOrBool; // string | boolean
|
||||||
|
>y2 : string | boolean
|
||||||
|
>strOrBool : string | boolean
|
||||||
|
}
|
||||||
|
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
|
@ -142,21 +143,6 @@ else {
|
||||||
>num : number
|
>num : number
|
||||||
>strOrNum : number
|
>strOrNum : number
|
||||||
}
|
}
|
||||||
if (typeof strOrBool !== "number") {
|
|
||||||
>typeof strOrBool !== "number" : boolean
|
|
||||||
>typeof strOrBool : string
|
|
||||||
>strOrBool : string | boolean
|
|
||||||
|
|
||||||
var y: string | boolean = strOrBool; // string | boolean
|
|
||||||
>y : string | boolean
|
|
||||||
>strOrBool : string | boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
num = strOrBool; // number
|
|
||||||
>num = strOrBool : number
|
|
||||||
>num : number
|
|
||||||
>strOrBool : number
|
|
||||||
}
|
|
||||||
if (typeof numOrBool !== "number") {
|
if (typeof numOrBool !== "number") {
|
||||||
>typeof numOrBool !== "number" : boolean
|
>typeof numOrBool !== "number" : boolean
|
||||||
>typeof numOrBool : string
|
>typeof numOrBool : string
|
||||||
|
@ -204,3 +190,20 @@ else {
|
||||||
>num : number
|
>num : number
|
||||||
>numOrC : number
|
>numOrC : number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrBool !== "number") {
|
||||||
|
>typeof strOrBool !== "number" : boolean
|
||||||
|
>typeof strOrBool : string
|
||||||
|
>strOrBool : string | boolean
|
||||||
|
|
||||||
|
var y1: string | boolean = strOrBool; // string | boolean
|
||||||
|
>y1 : string | boolean
|
||||||
|
>strOrBool : string | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var y2: string | boolean = strOrBool; // string | boolean
|
||||||
|
>y2 : string | boolean
|
||||||
|
>strOrBool : string | boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,12 +19,6 @@ var c: C;
|
||||||
// - when true, removes the primitive types string, number, and boolean from the type of x, or
|
// - when true, removes the primitive types string, number, and boolean from the type of x, or
|
||||||
// - when false, has no effect on the type of x.
|
// - when false, has no effect on the type of x.
|
||||||
|
|
||||||
if (typeof strOrNumOrBool === "Object") {
|
|
||||||
emptyObj = strOrNumOrBool; // {}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
|
||||||
}
|
|
||||||
if (typeof strOrC === "Object") {
|
if (typeof strOrC === "Object") {
|
||||||
c = strOrC; // C
|
c = strOrC; // C
|
||||||
}
|
}
|
||||||
|
@ -44,15 +38,17 @@ else {
|
||||||
var r4: boolean | C = boolOrC; // boolean | C
|
var r4: boolean | C = boolOrC; // boolean | C
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrNumOrBool === "Object") {
|
||||||
|
var q1: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var q2: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
||||||
|
}
|
||||||
|
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
if (typeof strOrNumOrBool !== "Object") {
|
|
||||||
var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
emptyObj = strOrNumOrBool; // {}
|
|
||||||
}
|
|
||||||
if (typeof strOrC !== "Object") {
|
if (typeof strOrC !== "Object") {
|
||||||
var r2: string | C = strOrC; // string | C
|
var r2: string | C = strOrC; // string | C
|
||||||
}
|
}
|
||||||
|
@ -70,7 +66,16 @@ if (typeof boolOrC !== "Object") {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
c = boolOrC; // C
|
c = boolOrC; // C
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrNumOrBool !== "Object") {
|
||||||
|
var q1: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var q2: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//// [typeGuardOfFormTypeOfOther.js]
|
//// [typeGuardOfFormTypeOfOther.js]
|
||||||
var C = (function () {
|
var C = (function () {
|
||||||
|
@ -95,12 +100,6 @@ var c;
|
||||||
// where s is a string literal with any value but 'string', 'number' or 'boolean',
|
// where s is a string literal with any value but 'string', 'number' or 'boolean',
|
||||||
// - when true, removes the primitive types string, number, and boolean from the type of x, or
|
// - when true, removes the primitive types string, number, and boolean from the type of x, or
|
||||||
// - when false, has no effect on the type of x.
|
// - when false, has no effect on the type of x.
|
||||||
if (typeof strOrNumOrBool === "Object") {
|
|
||||||
emptyObj = strOrNumOrBool; // {}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var r1 = strOrNumOrBool; // string | number | boolean
|
|
||||||
}
|
|
||||||
if (typeof strOrC === "Object") {
|
if (typeof strOrC === "Object") {
|
||||||
c = strOrC; // C
|
c = strOrC; // C
|
||||||
}
|
}
|
||||||
|
@ -119,15 +118,16 @@ if (typeof boolOrC === "Object") {
|
||||||
else {
|
else {
|
||||||
var r4 = boolOrC; // boolean | C
|
var r4 = boolOrC; // boolean | C
|
||||||
}
|
}
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrNumOrBool === "Object") {
|
||||||
|
var q1 = strOrNumOrBool; // string | number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var q2 = strOrNumOrBool; // string | number | boolean
|
||||||
|
}
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
if (typeof strOrNumOrBool !== "Object") {
|
|
||||||
var r1 = strOrNumOrBool; // string | number | boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
emptyObj = strOrNumOrBool; // {}
|
|
||||||
}
|
|
||||||
if (typeof strOrC !== "Object") {
|
if (typeof strOrC !== "Object") {
|
||||||
var r2 = strOrC; // string | C
|
var r2 = strOrC; // string | C
|
||||||
}
|
}
|
||||||
|
@ -146,3 +146,10 @@ if (typeof boolOrC !== "Object") {
|
||||||
else {
|
else {
|
||||||
c = boolOrC; // C
|
c = boolOrC; // C
|
||||||
}
|
}
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrNumOrBool !== "Object") {
|
||||||
|
var q1 = strOrNumOrBool; // string | number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var q2 = strOrNumOrBool; // string | number | boolean
|
||||||
|
}
|
||||||
|
|
|
@ -48,21 +48,6 @@ var c: C;
|
||||||
// - when true, removes the primitive types string, number, and boolean from the type of x, or
|
// - when true, removes the primitive types string, number, and boolean from the type of x, or
|
||||||
// - when false, has no effect on the type of x.
|
// - when false, has no effect on the type of x.
|
||||||
|
|
||||||
if (typeof strOrNumOrBool === "Object") {
|
|
||||||
>typeof strOrNumOrBool === "Object" : boolean
|
|
||||||
>typeof strOrNumOrBool : string
|
|
||||||
>strOrNumOrBool : string | number | boolean
|
|
||||||
|
|
||||||
emptyObj = strOrNumOrBool; // {}
|
|
||||||
>emptyObj = strOrNumOrBool : {}
|
|
||||||
>emptyObj : {}
|
|
||||||
>strOrNumOrBool : {}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
|
||||||
>r1 : string | number | boolean
|
|
||||||
>strOrNumOrBool : string | number | boolean
|
|
||||||
}
|
|
||||||
if (typeof strOrC === "Object") {
|
if (typeof strOrC === "Object") {
|
||||||
>typeof strOrC === "Object" : boolean
|
>typeof strOrC === "Object" : boolean
|
||||||
>typeof strOrC : string
|
>typeof strOrC : string
|
||||||
|
@ -112,24 +97,25 @@ else {
|
||||||
>boolOrC : boolean | C
|
>boolOrC : boolean | C
|
||||||
}
|
}
|
||||||
|
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
if (typeof strOrNumOrBool === "Object") {
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
>typeof strOrNumOrBool === "Object" : boolean
|
||||||
if (typeof strOrNumOrBool !== "Object") {
|
|
||||||
>typeof strOrNumOrBool !== "Object" : boolean
|
|
||||||
>typeof strOrNumOrBool : string
|
>typeof strOrNumOrBool : string
|
||||||
>strOrNumOrBool : string | number | boolean
|
>strOrNumOrBool : string | number | boolean
|
||||||
|
|
||||||
var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
var q1: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
||||||
>r1 : string | number | boolean
|
>q1 : string | number | boolean
|
||||||
>strOrNumOrBool : string | number | boolean
|
>strOrNumOrBool : string | number | boolean
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
emptyObj = strOrNumOrBool; // {}
|
var q2: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
||||||
>emptyObj = strOrNumOrBool : {}
|
>q2 : string | number | boolean
|
||||||
>emptyObj : {}
|
>strOrNumOrBool : string | number | boolean
|
||||||
>strOrNumOrBool : {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
if (typeof strOrC !== "Object") {
|
if (typeof strOrC !== "Object") {
|
||||||
>typeof strOrC !== "Object" : boolean
|
>typeof strOrC !== "Object" : boolean
|
||||||
>typeof strOrC : string
|
>typeof strOrC : string
|
||||||
|
@ -178,3 +164,20 @@ else {
|
||||||
>c : C
|
>c : C
|
||||||
>boolOrC : C
|
>boolOrC : C
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrNumOrBool !== "Object") {
|
||||||
|
>typeof strOrNumOrBool !== "Object" : boolean
|
||||||
|
>typeof strOrNumOrBool : string
|
||||||
|
>strOrNumOrBool : string | number | boolean
|
||||||
|
|
||||||
|
var q1: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
||||||
|
>q1 : string | number | boolean
|
||||||
|
>strOrNumOrBool : string | number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var q2: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
||||||
|
>q2 : string | number | boolean
|
||||||
|
>strOrNumOrBool : string | number | boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,12 +29,6 @@ if (typeof strOrBool === "string") {
|
||||||
else {
|
else {
|
||||||
bool = strOrBool; // boolean
|
bool = strOrBool; // boolean
|
||||||
}
|
}
|
||||||
if (typeof numOrBool === "string") {
|
|
||||||
str = numOrBool; // string
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var x : number | boolean = numOrBool; // number | boolean
|
|
||||||
}
|
|
||||||
if (typeof strOrNumOrBool === "string") {
|
if (typeof strOrNumOrBool === "string") {
|
||||||
str = strOrNumOrBool; // string
|
str = strOrNumOrBool; // string
|
||||||
}
|
}
|
||||||
|
@ -48,6 +42,14 @@ else {
|
||||||
c = strOrC; // C
|
c = strOrC; // C
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof numOrBool === "string") {
|
||||||
|
var x1: number | boolean = numOrBool; // number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var x2: number | boolean = numOrBool; // number | boolean
|
||||||
|
}
|
||||||
|
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
|
@ -63,12 +65,6 @@ if (typeof strOrBool !== "string") {
|
||||||
else {
|
else {
|
||||||
str = strOrBool; // string
|
str = strOrBool; // string
|
||||||
}
|
}
|
||||||
if (typeof numOrBool !== "string") {
|
|
||||||
var x: number | boolean = numOrBool; // number | boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
str = numOrBool; // string
|
|
||||||
}
|
|
||||||
if (typeof strOrNumOrBool !== "string") {
|
if (typeof strOrNumOrBool !== "string") {
|
||||||
numOrBool = strOrNumOrBool; // number | boolean
|
numOrBool = strOrNumOrBool; // number | boolean
|
||||||
}
|
}
|
||||||
|
@ -80,7 +76,16 @@ if (typeof strOrC !== "string") {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
str = strOrC; // string
|
str = strOrC; // string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof numOrBool !== "string") {
|
||||||
|
var x1: number | boolean = numOrBool; // number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var x2: number | boolean = numOrBool; // number | boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//// [typeGuardOfFormTypeOfString.js]
|
//// [typeGuardOfFormTypeOfString.js]
|
||||||
var C = (function () {
|
var C = (function () {
|
||||||
|
@ -116,12 +121,6 @@ if (typeof strOrBool === "string") {
|
||||||
else {
|
else {
|
||||||
bool = strOrBool; // boolean
|
bool = strOrBool; // boolean
|
||||||
}
|
}
|
||||||
if (typeof numOrBool === "string") {
|
|
||||||
str = numOrBool; // string
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var x = numOrBool; // number | boolean
|
|
||||||
}
|
|
||||||
if (typeof strOrNumOrBool === "string") {
|
if (typeof strOrNumOrBool === "string") {
|
||||||
str = strOrNumOrBool; // string
|
str = strOrNumOrBool; // string
|
||||||
}
|
}
|
||||||
|
@ -134,6 +133,13 @@ if (typeof strOrC === "string") {
|
||||||
else {
|
else {
|
||||||
c = strOrC; // C
|
c = strOrC; // C
|
||||||
}
|
}
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof numOrBool === "string") {
|
||||||
|
var x1 = numOrBool; // number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var x2 = numOrBool; // number | boolean
|
||||||
|
}
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
|
@ -149,12 +155,6 @@ if (typeof strOrBool !== "string") {
|
||||||
else {
|
else {
|
||||||
str = strOrBool; // string
|
str = strOrBool; // string
|
||||||
}
|
}
|
||||||
if (typeof numOrBool !== "string") {
|
|
||||||
var x = numOrBool; // number | boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
str = numOrBool; // string
|
|
||||||
}
|
|
||||||
if (typeof strOrNumOrBool !== "string") {
|
if (typeof strOrNumOrBool !== "string") {
|
||||||
numOrBool = strOrNumOrBool; // number | boolean
|
numOrBool = strOrNumOrBool; // number | boolean
|
||||||
}
|
}
|
||||||
|
@ -167,3 +167,10 @@ if (typeof strOrC !== "string") {
|
||||||
else {
|
else {
|
||||||
str = strOrC; // string
|
str = strOrC; // string
|
||||||
}
|
}
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof numOrBool !== "string") {
|
||||||
|
var x1 = numOrBool; // number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var x2 = numOrBool; // number | boolean
|
||||||
|
}
|
||||||
|
|
|
@ -76,21 +76,6 @@ else {
|
||||||
>bool : boolean
|
>bool : boolean
|
||||||
>strOrBool : boolean
|
>strOrBool : boolean
|
||||||
}
|
}
|
||||||
if (typeof numOrBool === "string") {
|
|
||||||
>typeof numOrBool === "string" : boolean
|
|
||||||
>typeof numOrBool : string
|
|
||||||
>numOrBool : number | boolean
|
|
||||||
|
|
||||||
str = numOrBool; // string
|
|
||||||
>str = numOrBool : string
|
|
||||||
>str : string
|
|
||||||
>numOrBool : string
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var x : number | boolean = numOrBool; // number | boolean
|
|
||||||
>x : number | boolean
|
|
||||||
>numOrBool : number | boolean
|
|
||||||
}
|
|
||||||
if (typeof strOrNumOrBool === "string") {
|
if (typeof strOrNumOrBool === "string") {
|
||||||
>typeof strOrNumOrBool === "string" : boolean
|
>typeof strOrNumOrBool === "string" : boolean
|
||||||
>typeof strOrNumOrBool : string
|
>typeof strOrNumOrBool : string
|
||||||
|
@ -124,6 +109,22 @@ else {
|
||||||
>strOrC : C
|
>strOrC : C
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof numOrBool === "string") {
|
||||||
|
>typeof numOrBool === "string" : boolean
|
||||||
|
>typeof numOrBool : string
|
||||||
|
>numOrBool : number | boolean
|
||||||
|
|
||||||
|
var x1: number | boolean = numOrBool; // number | boolean
|
||||||
|
>x1 : number | boolean
|
||||||
|
>numOrBool : number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var x2: number | boolean = numOrBool; // number | boolean
|
||||||
|
>x2 : number | boolean
|
||||||
|
>numOrBool : number | boolean
|
||||||
|
}
|
||||||
|
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
|
@ -159,21 +160,6 @@ else {
|
||||||
>str : string
|
>str : string
|
||||||
>strOrBool : string
|
>strOrBool : string
|
||||||
}
|
}
|
||||||
if (typeof numOrBool !== "string") {
|
|
||||||
>typeof numOrBool !== "string" : boolean
|
|
||||||
>typeof numOrBool : string
|
|
||||||
>numOrBool : number | boolean
|
|
||||||
|
|
||||||
var x: number | boolean = numOrBool; // number | boolean
|
|
||||||
>x : number | boolean
|
|
||||||
>numOrBool : number | boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
str = numOrBool; // string
|
|
||||||
>str = numOrBool : string
|
|
||||||
>str : string
|
|
||||||
>numOrBool : string
|
|
||||||
}
|
|
||||||
if (typeof strOrNumOrBool !== "string") {
|
if (typeof strOrNumOrBool !== "string") {
|
||||||
>typeof strOrNumOrBool !== "string" : boolean
|
>typeof strOrNumOrBool !== "string" : boolean
|
||||||
>typeof strOrNumOrBool : string
|
>typeof strOrNumOrBool : string
|
||||||
|
@ -206,3 +192,20 @@ else {
|
||||||
>str : string
|
>str : string
|
||||||
>strOrC : string
|
>strOrC : string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof numOrBool !== "string") {
|
||||||
|
>typeof numOrBool !== "string" : boolean
|
||||||
|
>typeof numOrBool : string
|
||||||
|
>numOrBool : number | boolean
|
||||||
|
|
||||||
|
var x1: number | boolean = numOrBool; // number | boolean
|
||||||
|
>x1 : number | boolean
|
||||||
|
>numOrBool : number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var x2: number | boolean = numOrBool; // number | boolean
|
||||||
|
>x2 : number | boolean
|
||||||
|
>numOrBool : number | boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,23 +18,24 @@ function foo2(x: number | string) {
|
||||||
return x.length; // string
|
return x.length; // string
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
(function f() {
|
var f = function () {
|
||||||
x = 10;
|
return x * x;
|
||||||
})();
|
};
|
||||||
return x++; // number
|
|
||||||
}
|
}
|
||||||
|
x = "hello";
|
||||||
|
f();
|
||||||
}
|
}
|
||||||
function foo3(x: number | string) {
|
function foo3(x: number | string) {
|
||||||
if (typeof x === "string") {
|
if (typeof x === "string") {
|
||||||
return x.length; // string
|
return x.length; // string
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
(() => {
|
var f = () => x * x;
|
||||||
x = 10;
|
|
||||||
})();
|
|
||||||
return x++; // number
|
|
||||||
}
|
}
|
||||||
}
|
x = "hello";
|
||||||
|
f();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//// [typeGuardsDefeat.js]
|
//// [typeGuardsDefeat.js]
|
||||||
// Also note that it is possible to defeat a type guard by calling a function that changes the
|
// Also note that it is possible to defeat a type guard by calling a function that changes the
|
||||||
|
@ -56,20 +57,20 @@ function foo2(x) {
|
||||||
return x.length; // string
|
return x.length; // string
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
(function f() {
|
var f = function () {
|
||||||
x = 10;
|
return x * x;
|
||||||
})();
|
};
|
||||||
return x++; // number
|
|
||||||
}
|
}
|
||||||
|
x = "hello";
|
||||||
|
f();
|
||||||
}
|
}
|
||||||
function foo3(x) {
|
function foo3(x) {
|
||||||
if (typeof x === "string") {
|
if (typeof x === "string") {
|
||||||
return x.length; // string
|
return x.length; // string
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
(function () {
|
var f = function () { return x * x; };
|
||||||
x = 10;
|
|
||||||
})();
|
|
||||||
return x++; // number
|
|
||||||
}
|
}
|
||||||
|
x = "hello";
|
||||||
|
f();
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,21 +47,24 @@ function foo2(x: number | string) {
|
||||||
>length : number
|
>length : number
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
(function f() {
|
var f = function () {
|
||||||
>(function f() { x = 10; })() : void
|
>f : () => number
|
||||||
>(function f() { x = 10; }) : () => void
|
>function () { return x * x; } : () => number
|
||||||
>function f() { x = 10; } : () => void
|
|
||||||
>f : () => void
|
|
||||||
|
|
||||||
x = 10;
|
return x * x;
|
||||||
>x = 10 : number
|
>x * x : number
|
||||||
|
>x : number
|
||||||
|
>x : number
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
x = "hello";
|
||||||
|
>x = "hello" : string
|
||||||
>x : string | number
|
>x : string | number
|
||||||
|
|
||||||
})();
|
f();
|
||||||
return x++; // number
|
>f() : number
|
||||||
>x++ : number
|
>f : () => number
|
||||||
>x : number
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
function foo3(x: number | string) {
|
function foo3(x: number | string) {
|
||||||
>foo3 : (x: string | number) => number
|
>foo3 : (x: string | number) => number
|
||||||
|
@ -78,18 +81,19 @@ function foo3(x: number | string) {
|
||||||
>length : number
|
>length : number
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
(() => {
|
var f = () => x * x;
|
||||||
>(() => { x = 10; })() : void
|
>f : () => number
|
||||||
>(() => { x = 10; }) : () => void
|
>() => x * x : () => number
|
||||||
>() => { x = 10; } : () => void
|
>x * x : number
|
||||||
|
>x : number
|
||||||
x = 10;
|
|
||||||
>x = 10 : number
|
|
||||||
>x : string | number
|
|
||||||
|
|
||||||
})();
|
|
||||||
return x++; // number
|
|
||||||
>x++ : number
|
|
||||||
>x : number
|
>x : number
|
||||||
}
|
}
|
||||||
|
x = "hello";
|
||||||
|
>x = "hello" : string
|
||||||
|
>x : string | number
|
||||||
|
|
||||||
|
f();
|
||||||
|
>f() : number
|
||||||
|
>f : () => number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,40 +5,40 @@ function foo(x: number | string | boolean) {
|
||||||
return typeof x === "string"
|
return typeof x === "string"
|
||||||
? x
|
? x
|
||||||
: function f() {
|
: function f() {
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // number | boolean
|
||||||
return typeof x === "boolean"
|
return typeof x === "boolean"
|
||||||
? x.toString() // boolean
|
? x.toString() // boolean
|
||||||
: x.toString(); // number | string
|
: x.toString(); // number
|
||||||
} ();
|
} ();
|
||||||
}
|
}
|
||||||
function foo2(x: number | string | boolean) {
|
function foo2(x: number | string | boolean) {
|
||||||
return typeof x === "string"
|
return typeof x === "string"
|
||||||
? x
|
? x
|
||||||
: function f(a: number | boolean) {
|
: function f(a: number | boolean) {
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // new scope - number | boolean
|
||||||
return typeof x === "boolean"
|
return typeof x === "boolean"
|
||||||
? x.toString() // boolean
|
? x.toString() // boolean
|
||||||
: x.toString(); // number | string
|
: x.toString(); // number
|
||||||
} (x); // x here is narrowed to number | boolean
|
} (x); // x here is narrowed to number | boolean
|
||||||
}
|
}
|
||||||
function foo3(x: number | string | boolean) {
|
function foo3(x: number | string | boolean) {
|
||||||
return typeof x === "string"
|
return typeof x === "string"
|
||||||
? x
|
? x
|
||||||
: (() => {
|
: (() => {
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // new scope - number | boolean
|
||||||
return typeof x === "boolean"
|
return typeof x === "boolean"
|
||||||
? x.toString() // boolean
|
? x.toString() // boolean
|
||||||
: x.toString(); // number | string
|
: x.toString(); // number
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
function foo4(x: number | string | boolean) {
|
function foo4(x: number | string | boolean) {
|
||||||
return typeof x === "string"
|
return typeof x === "string"
|
||||||
? x
|
? x
|
||||||
: ((a: number | boolean) => {
|
: ((a: number | boolean) => {
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // new scope - number | boolean
|
||||||
return typeof x === "boolean"
|
return typeof x === "boolean"
|
||||||
? x.toString() // boolean
|
? x.toString() // boolean
|
||||||
: x.toString(); // number | string
|
: x.toString(); // number
|
||||||
})(x); // x here is narrowed to number | boolean
|
})(x); // x here is narrowed to number | boolean
|
||||||
}
|
}
|
||||||
module m {
|
module m {
|
||||||
|
@ -74,26 +74,26 @@ module m1 {
|
||||||
// typeguards are scoped in function/module block
|
// typeguards are scoped in function/module block
|
||||||
function foo(x) {
|
function foo(x) {
|
||||||
return typeof x === "string" ? x : function f() {
|
return typeof x === "string" ? x : function f() {
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // number | boolean
|
||||||
return typeof x === "boolean" ? x.toString() : x.toString(); // number | string
|
return typeof x === "boolean" ? x.toString() : x.toString(); // number
|
||||||
}();
|
}();
|
||||||
}
|
}
|
||||||
function foo2(x) {
|
function foo2(x) {
|
||||||
return typeof x === "string" ? x : function f(a) {
|
return typeof x === "string" ? x : function f(a) {
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // new scope - number | boolean
|
||||||
return typeof x === "boolean" ? x.toString() : x.toString(); // number | string
|
return typeof x === "boolean" ? x.toString() : x.toString(); // number
|
||||||
}(x); // x here is narrowed to number | boolean
|
}(x); // x here is narrowed to number | boolean
|
||||||
}
|
}
|
||||||
function foo3(x) {
|
function foo3(x) {
|
||||||
return typeof x === "string" ? x : (function () {
|
return typeof x === "string" ? x : (function () {
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // new scope - number | boolean
|
||||||
return typeof x === "boolean" ? x.toString() : x.toString(); // number | string
|
return typeof x === "boolean" ? x.toString() : x.toString(); // number
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
function foo4(x) {
|
function foo4(x) {
|
||||||
return typeof x === "string" ? x : (function (a) {
|
return typeof x === "string" ? x : (function (a) {
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // new scope - number | boolean
|
||||||
return typeof x === "boolean" ? x.toString() : x.toString(); // number | string
|
return typeof x === "boolean" ? x.toString() : x.toString(); // number
|
||||||
})(x); // x here is narrowed to number | boolean
|
})(x); // x here is narrowed to number | boolean
|
||||||
}
|
}
|
||||||
var m;
|
var m;
|
||||||
|
|
|
@ -6,7 +6,7 @@ function foo(x: number | string | boolean) {
|
||||||
>x : string | number | boolean
|
>x : string | number | boolean
|
||||||
|
|
||||||
return typeof x === "string"
|
return typeof x === "string"
|
||||||
>typeof x === "string" ? x : function f() { var b = x; // new scope - number | boolean | string return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number | string } () : string
|
>typeof x === "string" ? x : function f() { var b = x; // number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } () : string
|
||||||
>typeof x === "string" : boolean
|
>typeof x === "string" : boolean
|
||||||
>typeof x : string
|
>typeof x : string
|
||||||
>x : string | number | boolean
|
>x : string | number | boolean
|
||||||
|
@ -15,19 +15,19 @@ function foo(x: number | string | boolean) {
|
||||||
>x : string
|
>x : string
|
||||||
|
|
||||||
: function f() {
|
: function f() {
|
||||||
>function f() { var b = x; // new scope - number | boolean | string return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number | string } () : string
|
>function f() { var b = x; // number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } () : string
|
||||||
>function f() { var b = x; // new scope - number | boolean | string return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number | string } : () => string
|
>function f() { var b = x; // number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } : () => string
|
||||||
>f : () => string
|
>f : () => string
|
||||||
|
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // number | boolean
|
||||||
>b : string | number | boolean
|
>b : number | boolean
|
||||||
>x : string | number | boolean
|
>x : number | boolean
|
||||||
|
|
||||||
return typeof x === "boolean"
|
return typeof x === "boolean"
|
||||||
>typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
|
>typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
|
||||||
>typeof x === "boolean" : boolean
|
>typeof x === "boolean" : boolean
|
||||||
>typeof x : string
|
>typeof x : string
|
||||||
>x : string | number | boolean
|
>x : number | boolean
|
||||||
|
|
||||||
? x.toString() // boolean
|
? x.toString() // boolean
|
||||||
>x.toString() : string
|
>x.toString() : string
|
||||||
|
@ -35,11 +35,11 @@ function foo(x: number | string | boolean) {
|
||||||
>x : boolean
|
>x : boolean
|
||||||
>toString : () => string
|
>toString : () => string
|
||||||
|
|
||||||
: x.toString(); // number | string
|
: x.toString(); // number
|
||||||
>x.toString() : string
|
>x.toString() : string
|
||||||
>x.toString : () => string
|
>x.toString : (radix?: number) => string
|
||||||
>x : string | number
|
>x : number
|
||||||
>toString : () => string
|
>toString : (radix?: number) => string
|
||||||
|
|
||||||
} ();
|
} ();
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,7 @@ function foo2(x: number | string | boolean) {
|
||||||
>x : string | number | boolean
|
>x : string | number | boolean
|
||||||
|
|
||||||
return typeof x === "string"
|
return typeof x === "string"
|
||||||
>typeof x === "string" ? x : function f(a: number | boolean) { var b = x; // new scope - number | boolean | string return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number | string } (x) : string
|
>typeof x === "string" ? x : function f(a: number | boolean) { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } (x) : string
|
||||||
>typeof x === "string" : boolean
|
>typeof x === "string" : boolean
|
||||||
>typeof x : string
|
>typeof x : string
|
||||||
>x : string | number | boolean
|
>x : string | number | boolean
|
||||||
|
@ -57,20 +57,20 @@ function foo2(x: number | string | boolean) {
|
||||||
>x : string
|
>x : string
|
||||||
|
|
||||||
: function f(a: number | boolean) {
|
: function f(a: number | boolean) {
|
||||||
>function f(a: number | boolean) { var b = x; // new scope - number | boolean | string return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number | string } (x) : string
|
>function f(a: number | boolean) { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } (x) : string
|
||||||
>function f(a: number | boolean) { var b = x; // new scope - number | boolean | string return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number | string } : (a: number | boolean) => string
|
>function f(a: number | boolean) { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } : (a: number | boolean) => string
|
||||||
>f : (a: number | boolean) => string
|
>f : (a: number | boolean) => string
|
||||||
>a : number | boolean
|
>a : number | boolean
|
||||||
|
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // new scope - number | boolean
|
||||||
>b : string | number | boolean
|
>b : number | boolean
|
||||||
>x : string | number | boolean
|
>x : number | boolean
|
||||||
|
|
||||||
return typeof x === "boolean"
|
return typeof x === "boolean"
|
||||||
>typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
|
>typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
|
||||||
>typeof x === "boolean" : boolean
|
>typeof x === "boolean" : boolean
|
||||||
>typeof x : string
|
>typeof x : string
|
||||||
>x : string | number | boolean
|
>x : number | boolean
|
||||||
|
|
||||||
? x.toString() // boolean
|
? x.toString() // boolean
|
||||||
>x.toString() : string
|
>x.toString() : string
|
||||||
|
@ -78,11 +78,11 @@ function foo2(x: number | string | boolean) {
|
||||||
>x : boolean
|
>x : boolean
|
||||||
>toString : () => string
|
>toString : () => string
|
||||||
|
|
||||||
: x.toString(); // number | string
|
: x.toString(); // number
|
||||||
>x.toString() : string
|
>x.toString() : string
|
||||||
>x.toString : () => string
|
>x.toString : (radix?: number) => string
|
||||||
>x : string | number
|
>x : number
|
||||||
>toString : () => string
|
>toString : (radix?: number) => string
|
||||||
|
|
||||||
} (x); // x here is narrowed to number | boolean
|
} (x); // x here is narrowed to number | boolean
|
||||||
>x : number | boolean
|
>x : number | boolean
|
||||||
|
@ -92,7 +92,7 @@ function foo3(x: number | string | boolean) {
|
||||||
>x : string | number | boolean
|
>x : string | number | boolean
|
||||||
|
|
||||||
return typeof x === "string"
|
return typeof x === "string"
|
||||||
>typeof x === "string" ? x : (() => { var b = x; // new scope - number | boolean | string return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number | string })() : string
|
>typeof x === "string" ? x : (() => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number })() : string
|
||||||
>typeof x === "string" : boolean
|
>typeof x === "string" : boolean
|
||||||
>typeof x : string
|
>typeof x : string
|
||||||
>x : string | number | boolean
|
>x : string | number | boolean
|
||||||
|
@ -101,19 +101,19 @@ function foo3(x: number | string | boolean) {
|
||||||
>x : string
|
>x : string
|
||||||
|
|
||||||
: (() => {
|
: (() => {
|
||||||
>(() => { var b = x; // new scope - number | boolean | string return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number | string })() : string
|
>(() => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number })() : string
|
||||||
>(() => { var b = x; // new scope - number | boolean | string return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number | string }) : () => string
|
>(() => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number }) : () => string
|
||||||
>() => { var b = x; // new scope - number | boolean | string return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number | string } : () => string
|
>() => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } : () => string
|
||||||
|
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // new scope - number | boolean
|
||||||
>b : string | number | boolean
|
>b : number | boolean
|
||||||
>x : string | number | boolean
|
>x : number | boolean
|
||||||
|
|
||||||
return typeof x === "boolean"
|
return typeof x === "boolean"
|
||||||
>typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
|
>typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
|
||||||
>typeof x === "boolean" : boolean
|
>typeof x === "boolean" : boolean
|
||||||
>typeof x : string
|
>typeof x : string
|
||||||
>x : string | number | boolean
|
>x : number | boolean
|
||||||
|
|
||||||
? x.toString() // boolean
|
? x.toString() // boolean
|
||||||
>x.toString() : string
|
>x.toString() : string
|
||||||
|
@ -121,11 +121,11 @@ function foo3(x: number | string | boolean) {
|
||||||
>x : boolean
|
>x : boolean
|
||||||
>toString : () => string
|
>toString : () => string
|
||||||
|
|
||||||
: x.toString(); // number | string
|
: x.toString(); // number
|
||||||
>x.toString() : string
|
>x.toString() : string
|
||||||
>x.toString : () => string
|
>x.toString : (radix?: number) => string
|
||||||
>x : string | number
|
>x : number
|
||||||
>toString : () => string
|
>toString : (radix?: number) => string
|
||||||
|
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
|
@ -134,7 +134,7 @@ function foo4(x: number | string | boolean) {
|
||||||
>x : string | number | boolean
|
>x : string | number | boolean
|
||||||
|
|
||||||
return typeof x === "string"
|
return typeof x === "string"
|
||||||
>typeof x === "string" ? x : ((a: number | boolean) => { var b = x; // new scope - number | boolean | string return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number | string })(x) : string
|
>typeof x === "string" ? x : ((a: number | boolean) => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number })(x) : string
|
||||||
>typeof x === "string" : boolean
|
>typeof x === "string" : boolean
|
||||||
>typeof x : string
|
>typeof x : string
|
||||||
>x : string | number | boolean
|
>x : string | number | boolean
|
||||||
|
@ -143,20 +143,20 @@ function foo4(x: number | string | boolean) {
|
||||||
>x : string
|
>x : string
|
||||||
|
|
||||||
: ((a: number | boolean) => {
|
: ((a: number | boolean) => {
|
||||||
>((a: number | boolean) => { var b = x; // new scope - number | boolean | string return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number | string })(x) : string
|
>((a: number | boolean) => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number })(x) : string
|
||||||
>((a: number | boolean) => { var b = x; // new scope - number | boolean | string return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number | string }) : (a: number | boolean) => string
|
>((a: number | boolean) => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number }) : (a: number | boolean) => string
|
||||||
>(a: number | boolean) => { var b = x; // new scope - number | boolean | string return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number | string } : (a: number | boolean) => string
|
>(a: number | boolean) => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } : (a: number | boolean) => string
|
||||||
>a : number | boolean
|
>a : number | boolean
|
||||||
|
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // new scope - number | boolean
|
||||||
>b : string | number | boolean
|
>b : number | boolean
|
||||||
>x : string | number | boolean
|
>x : number | boolean
|
||||||
|
|
||||||
return typeof x === "boolean"
|
return typeof x === "boolean"
|
||||||
>typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
|
>typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
|
||||||
>typeof x === "boolean" : boolean
|
>typeof x === "boolean" : boolean
|
||||||
>typeof x : string
|
>typeof x : string
|
||||||
>x : string | number | boolean
|
>x : number | boolean
|
||||||
|
|
||||||
? x.toString() // boolean
|
? x.toString() // boolean
|
||||||
>x.toString() : string
|
>x.toString() : string
|
||||||
|
@ -164,11 +164,11 @@ function foo4(x: number | string | boolean) {
|
||||||
>x : boolean
|
>x : boolean
|
||||||
>toString : () => string
|
>toString : () => string
|
||||||
|
|
||||||
: x.toString(); // number | string
|
: x.toString(); // number
|
||||||
>x.toString() : string
|
>x.toString() : string
|
||||||
>x.toString : () => string
|
>x.toString : (radix?: number) => string
|
||||||
>x : string | number
|
>x : number
|
||||||
>toString : () => string
|
>toString : (radix?: number) => string
|
||||||
|
|
||||||
})(x); // x here is narrowed to number | boolean
|
})(x); // x here is narrowed to number | boolean
|
||||||
>x : number | boolean
|
>x : number | boolean
|
||||||
|
|
|
@ -16,12 +16,6 @@ var c: C;
|
||||||
// where s is a string literal with the value 'string', 'number', or 'boolean',
|
// where s is a string literal with the value 'string', 'number', or 'boolean',
|
||||||
// - when true, narrows the type of x to the given primitive type, or
|
// - when true, narrows the type of x to the given primitive type, or
|
||||||
// - when false, removes the primitive type from the type of x.
|
// - when false, removes the primitive type from the type of x.
|
||||||
if (typeof strOrNum === "boolean") {
|
|
||||||
bool = strOrNum; // boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var z: string | number = strOrNum; // string | number
|
|
||||||
}
|
|
||||||
if (typeof strOrBool === "boolean") {
|
if (typeof strOrBool === "boolean") {
|
||||||
bool = strOrBool; // boolean
|
bool = strOrBool; // boolean
|
||||||
}
|
}
|
||||||
|
@ -47,15 +41,18 @@ else {
|
||||||
c = boolOrC; // C
|
c = boolOrC; // C
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrNum === "boolean") {
|
||||||
|
var z1: string | number = strOrNum; // string | number
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var z2: string | number = strOrNum; // string | number
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
if (typeof strOrNum !== "boolean") {
|
|
||||||
var z: string | number = strOrNum; // string | number
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
bool = strOrNum; // boolean
|
|
||||||
}
|
|
||||||
if (typeof strOrBool !== "boolean") {
|
if (typeof strOrBool !== "boolean") {
|
||||||
str = strOrBool; // string
|
str = strOrBool; // string
|
||||||
}
|
}
|
||||||
|
@ -79,4 +76,12 @@ if (typeof boolOrC !== "boolean") {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
bool = boolOrC; // boolean
|
bool = boolOrC; // boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrNum !== "boolean") {
|
||||||
|
var z1: string | number = strOrNum; // string | number
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var z2: string | number = strOrNum; // string | number
|
||||||
|
}
|
||||||
|
|
|
@ -22,12 +22,6 @@ if (typeof strOrNum === "number") {
|
||||||
else {
|
else {
|
||||||
str === strOrNum; // string
|
str === strOrNum; // string
|
||||||
}
|
}
|
||||||
if (typeof strOrBool === "number") {
|
|
||||||
num = strOrBool; // number
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var y: string | boolean = strOrBool; // string | boolean
|
|
||||||
}
|
|
||||||
if (typeof numOrBool === "number") {
|
if (typeof numOrBool === "number") {
|
||||||
num = numOrBool; // number
|
num = numOrBool; // number
|
||||||
}
|
}
|
||||||
|
@ -47,6 +41,14 @@ else {
|
||||||
c = numOrC; // C
|
c = numOrC; // C
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrBool === "number") {
|
||||||
|
var y1: string | boolean = strOrBool; // string | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var y2: string | boolean = strOrBool; // string | boolean
|
||||||
|
}
|
||||||
|
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
|
@ -56,12 +58,6 @@ if (typeof strOrNum !== "number") {
|
||||||
else {
|
else {
|
||||||
num = strOrNum; // number
|
num = strOrNum; // number
|
||||||
}
|
}
|
||||||
if (typeof strOrBool !== "number") {
|
|
||||||
var y: string | boolean = strOrBool; // string | boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
num = strOrBool; // number
|
|
||||||
}
|
|
||||||
if (typeof numOrBool !== "number") {
|
if (typeof numOrBool !== "number") {
|
||||||
var x: number | boolean = numOrBool; // number | boolean
|
var x: number | boolean = numOrBool; // number | boolean
|
||||||
}
|
}
|
||||||
|
@ -79,4 +75,12 @@ if (typeof numOrC !== "number") {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
num = numOrC; // number
|
num = numOrC; // number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrBool !== "number") {
|
||||||
|
var y1: string | boolean = strOrBool; // string | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var y2: string | boolean = strOrBool; // string | boolean
|
||||||
|
}
|
||||||
|
|
|
@ -18,12 +18,6 @@ var c: C;
|
||||||
// - when true, removes the primitive types string, number, and boolean from the type of x, or
|
// - when true, removes the primitive types string, number, and boolean from the type of x, or
|
||||||
// - when false, has no effect on the type of x.
|
// - when false, has no effect on the type of x.
|
||||||
|
|
||||||
if (typeof strOrNumOrBool === "Object") {
|
|
||||||
emptyObj = strOrNumOrBool; // {}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
|
||||||
}
|
|
||||||
if (typeof strOrC === "Object") {
|
if (typeof strOrC === "Object") {
|
||||||
c = strOrC; // C
|
c = strOrC; // C
|
||||||
}
|
}
|
||||||
|
@ -43,15 +37,17 @@ else {
|
||||||
var r4: boolean | C = boolOrC; // boolean | C
|
var r4: boolean | C = boolOrC; // boolean | C
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrNumOrBool === "Object") {
|
||||||
|
var q1: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var q2: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
||||||
|
}
|
||||||
|
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
if (typeof strOrNumOrBool !== "Object") {
|
|
||||||
var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
emptyObj = strOrNumOrBool; // {}
|
|
||||||
}
|
|
||||||
if (typeof strOrC !== "Object") {
|
if (typeof strOrC !== "Object") {
|
||||||
var r2: string | C = strOrC; // string | C
|
var r2: string | C = strOrC; // string | C
|
||||||
}
|
}
|
||||||
|
@ -69,4 +65,12 @@ if (typeof boolOrC !== "Object") {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
c = boolOrC; // C
|
c = boolOrC; // C
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof strOrNumOrBool !== "Object") {
|
||||||
|
var q1: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var q2: string | number | boolean = strOrNumOrBool; // string | number | boolean
|
||||||
|
}
|
||||||
|
|
|
@ -28,12 +28,6 @@ if (typeof strOrBool === "string") {
|
||||||
else {
|
else {
|
||||||
bool = strOrBool; // boolean
|
bool = strOrBool; // boolean
|
||||||
}
|
}
|
||||||
if (typeof numOrBool === "string") {
|
|
||||||
str = numOrBool; // string
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var x : number | boolean = numOrBool; // number | boolean
|
|
||||||
}
|
|
||||||
if (typeof strOrNumOrBool === "string") {
|
if (typeof strOrNumOrBool === "string") {
|
||||||
str = strOrNumOrBool; // string
|
str = strOrNumOrBool; // string
|
||||||
}
|
}
|
||||||
|
@ -47,6 +41,14 @@ else {
|
||||||
c = strOrC; // C
|
c = strOrC; // C
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof numOrBool === "string") {
|
||||||
|
var x1: number | boolean = numOrBool; // number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var x2: number | boolean = numOrBool; // number | boolean
|
||||||
|
}
|
||||||
|
|
||||||
// A type guard of the form typeof x !== s, where s is a string literal,
|
// A type guard of the form typeof x !== s, where s is a string literal,
|
||||||
// - when true, narrows the type of x by typeof x === s when false, or
|
// - when true, narrows the type of x by typeof x === s when false, or
|
||||||
// - when false, narrows the type of x by typeof x === s when true.
|
// - when false, narrows the type of x by typeof x === s when true.
|
||||||
|
@ -62,12 +64,6 @@ if (typeof strOrBool !== "string") {
|
||||||
else {
|
else {
|
||||||
str = strOrBool; // string
|
str = strOrBool; // string
|
||||||
}
|
}
|
||||||
if (typeof numOrBool !== "string") {
|
|
||||||
var x: number | boolean = numOrBool; // number | boolean
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
str = numOrBool; // string
|
|
||||||
}
|
|
||||||
if (typeof strOrNumOrBool !== "string") {
|
if (typeof strOrNumOrBool !== "string") {
|
||||||
numOrBool = strOrNumOrBool; // number | boolean
|
numOrBool = strOrNumOrBool; // number | boolean
|
||||||
}
|
}
|
||||||
|
@ -79,4 +75,12 @@ if (typeof strOrC !== "string") {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
str = strOrC; // string
|
str = strOrC; // string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Narrowing occurs only if target type is a subtype of variable type
|
||||||
|
if (typeof numOrBool !== "string") {
|
||||||
|
var x1: number | boolean = numOrBool; // number | boolean
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var x2: number | boolean = numOrBool; // number | boolean
|
||||||
|
}
|
||||||
|
|
|
@ -17,20 +17,20 @@ function foo2(x: number | string) {
|
||||||
return x.length; // string
|
return x.length; // string
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
(function f() {
|
var f = function () {
|
||||||
x = 10;
|
return x * x;
|
||||||
})();
|
};
|
||||||
return x++; // number
|
|
||||||
}
|
}
|
||||||
|
x = "hello";
|
||||||
|
f();
|
||||||
}
|
}
|
||||||
function foo3(x: number | string) {
|
function foo3(x: number | string) {
|
||||||
if (typeof x === "string") {
|
if (typeof x === "string") {
|
||||||
return x.length; // string
|
return x.length; // string
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
(() => {
|
var f = () => x * x;
|
||||||
x = 10;
|
|
||||||
})();
|
|
||||||
return x++; // number
|
|
||||||
}
|
}
|
||||||
}
|
x = "hello";
|
||||||
|
f();
|
||||||
|
}
|
||||||
|
|
|
@ -4,40 +4,40 @@ function foo(x: number | string | boolean) {
|
||||||
return typeof x === "string"
|
return typeof x === "string"
|
||||||
? x
|
? x
|
||||||
: function f() {
|
: function f() {
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // number | boolean
|
||||||
return typeof x === "boolean"
|
return typeof x === "boolean"
|
||||||
? x.toString() // boolean
|
? x.toString() // boolean
|
||||||
: x.toString(); // number | string
|
: x.toString(); // number
|
||||||
} ();
|
} ();
|
||||||
}
|
}
|
||||||
function foo2(x: number | string | boolean) {
|
function foo2(x: number | string | boolean) {
|
||||||
return typeof x === "string"
|
return typeof x === "string"
|
||||||
? x
|
? x
|
||||||
: function f(a: number | boolean) {
|
: function f(a: number | boolean) {
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // new scope - number | boolean
|
||||||
return typeof x === "boolean"
|
return typeof x === "boolean"
|
||||||
? x.toString() // boolean
|
? x.toString() // boolean
|
||||||
: x.toString(); // number | string
|
: x.toString(); // number
|
||||||
} (x); // x here is narrowed to number | boolean
|
} (x); // x here is narrowed to number | boolean
|
||||||
}
|
}
|
||||||
function foo3(x: number | string | boolean) {
|
function foo3(x: number | string | boolean) {
|
||||||
return typeof x === "string"
|
return typeof x === "string"
|
||||||
? x
|
? x
|
||||||
: (() => {
|
: (() => {
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // new scope - number | boolean
|
||||||
return typeof x === "boolean"
|
return typeof x === "boolean"
|
||||||
? x.toString() // boolean
|
? x.toString() // boolean
|
||||||
: x.toString(); // number | string
|
: x.toString(); // number
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
function foo4(x: number | string | boolean) {
|
function foo4(x: number | string | boolean) {
|
||||||
return typeof x === "string"
|
return typeof x === "string"
|
||||||
? x
|
? x
|
||||||
: ((a: number | boolean) => {
|
: ((a: number | boolean) => {
|
||||||
var b = x; // new scope - number | boolean | string
|
var b = x; // new scope - number | boolean
|
||||||
return typeof x === "boolean"
|
return typeof x === "boolean"
|
||||||
? x.toString() // boolean
|
? x.toString() // boolean
|
||||||
: x.toString(); // number | string
|
: x.toString(); // number
|
||||||
})(x); // x here is narrowed to number | boolean
|
})(x); // x here is narrowed to number | boolean
|
||||||
}
|
}
|
||||||
module m {
|
module m {
|
||||||
|
|
Loading…
Reference in a new issue