string literal case test

This commit is contained in:
Wesley Wigham
2016-03-11 02:24:26 -05:00
parent 20d8a94d66
commit a2feb0ee7d
4 changed files with 98 additions and 0 deletions
@@ -0,0 +1,21 @@
//// [typeGuardNarrowsToLiteralType.ts]
declare function isFoo(value: string) : value is "foo";
declare function doThis(value: "foo"): void;
declare function doThat(value: string) : void;
let value: string;
if (isFoo(value)) {
doThis(value);
} else {
doThat(value);
}
//// [typeGuardNarrowsToLiteralType.js]
var value;
if (isFoo(value)) {
doThis(value);
}
else {
doThat(value);
}
@@ -0,0 +1,32 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts ===
declare function isFoo(value: string) : value is "foo";
>isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralType.ts, 0, 0))
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 0, 23))
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 0, 23))
declare function doThis(value: "foo"): void;
>doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralType.ts, 0, 55))
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 1, 24))
declare function doThat(value: string) : void;
>doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralType.ts, 1, 44))
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 2, 24))
let value: string;
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3))
if (isFoo(value)) {
>isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralType.ts, 0, 0))
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3))
doThis(value);
>doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralType.ts, 0, 55))
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3))
} else {
doThat(value);
>doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralType.ts, 1, 44))
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralType.ts, 3, 3))
}
@@ -0,0 +1,35 @@
=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralType.ts ===
declare function isFoo(value: string) : value is "foo";
>isFoo : (value: string) => value is "foo"
>value : string
>value : any
declare function doThis(value: "foo"): void;
>doThis : (value: "foo") => void
>value : "foo"
declare function doThat(value: string) : void;
>doThat : (value: string) => void
>value : string
let value: string;
>value : string
if (isFoo(value)) {
>isFoo(value) : boolean
>isFoo : (value: string) => value is "foo"
>value : string
doThis(value);
>doThis(value) : void
>doThis : (value: "foo") => void
>value : "foo"
} else {
doThat(value);
>doThat(value) : void
>doThat : (value: string) => void
>value : string
}
@@ -0,0 +1,10 @@
declare function isFoo(value: string) : value is "foo";
declare function doThis(value: "foo"): void;
declare function doThat(value: string) : void;
let value: string;
if (isFoo(value)) {
doThis(value);
} else {
doThat(value);
}