mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Add test for narrowing to unions of string literals
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
//// [typeGuardNarrowsToLiteralTypeUnion.ts]
|
||||
declare function isFoo(value: string) : value is ("foo" | "bar");
|
||||
declare function doThis(value: "foo" | "bar"): void;
|
||||
declare function doThat(value: string) : void;
|
||||
let value: string;
|
||||
if (isFoo(value)) {
|
||||
doThis(value);
|
||||
} else {
|
||||
doThat(value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//// [typeGuardNarrowsToLiteralTypeUnion.js]
|
||||
var value;
|
||||
if (isFoo(value)) {
|
||||
doThis(value);
|
||||
}
|
||||
else {
|
||||
doThat(value);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts ===
|
||||
declare function isFoo(value: string) : value is ("foo" | "bar");
|
||||
>isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 0))
|
||||
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 23))
|
||||
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 23))
|
||||
|
||||
declare function doThis(value: "foo" | "bar"): void;
|
||||
>doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 65))
|
||||
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 1, 24))
|
||||
|
||||
declare function doThat(value: string) : void;
|
||||
>doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 1, 52))
|
||||
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 2, 24))
|
||||
|
||||
let value: string;
|
||||
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3))
|
||||
|
||||
if (isFoo(value)) {
|
||||
>isFoo : Symbol(isFoo, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 0))
|
||||
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3))
|
||||
|
||||
doThis(value);
|
||||
>doThis : Symbol(doThis, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 0, 65))
|
||||
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3))
|
||||
|
||||
} else {
|
||||
doThat(value);
|
||||
>doThat : Symbol(doThat, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 1, 52))
|
||||
>value : Symbol(value, Decl(typeGuardNarrowsToLiteralTypeUnion.ts, 3, 3))
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
=== tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts ===
|
||||
declare function isFoo(value: string) : value is ("foo" | "bar");
|
||||
>isFoo : (value: string) => value is "foo" | "bar"
|
||||
>value : string
|
||||
>value : any
|
||||
|
||||
declare function doThis(value: "foo" | "bar"): void;
|
||||
>doThis : (value: "foo" | "bar") => void
|
||||
>value : "foo" | "bar"
|
||||
|
||||
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" | "bar"
|
||||
>value : string
|
||||
|
||||
doThis(value);
|
||||
>doThis(value) : void
|
||||
>doThis : (value: "foo" | "bar") => void
|
||||
>value : "foo" | "bar"
|
||||
|
||||
} else {
|
||||
doThat(value);
|
||||
>doThat(value) : void
|
||||
>doThat : (value: string) => void
|
||||
>value : string
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
declare function isFoo(value: string) : value is ("foo" | "bar");
|
||||
declare function doThis(value: "foo" | "bar"): void;
|
||||
declare function doThat(value: string) : void;
|
||||
let value: string;
|
||||
if (isFoo(value)) {
|
||||
doThis(value);
|
||||
} else {
|
||||
doThat(value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user