Added tests for operations that use assignable to/from.
This commit is contained in:
parent
049d02f871
commit
6618fd21bf
|
@ -0,0 +1,17 @@
|
||||||
|
|
||||||
|
type S = "a" | "b";
|
||||||
|
type T = S[] | S;
|
||||||
|
|
||||||
|
function f(foo: T) {
|
||||||
|
if (foo === "a") {
|
||||||
|
return foo;
|
||||||
|
}
|
||||||
|
else if (foo === "b") {
|
||||||
|
return foo;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return (foo as S[])[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error("Unreachable code hit.");
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
|
||||||
|
type S = "a" | "b";
|
||||||
|
type T = S[] | S;
|
||||||
|
|
||||||
|
function isS(t: T): t is S {
|
||||||
|
return t === "a" || t === "b";
|
||||||
|
}
|
||||||
|
|
||||||
|
function f(foo: T) {
|
||||||
|
if (isS(foo)) {
|
||||||
|
return foo;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return foo[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error("Unreachable code hit.");
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
|
||||||
|
type S = "a" | "b";
|
||||||
|
type T = S[] | S;
|
||||||
|
|
||||||
|
var foo: T;
|
||||||
|
switch (foo) {
|
||||||
|
case "a":
|
||||||
|
case "b":
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
foo = (foo as S[])[0];
|
||||||
|
break;
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
|
||||||
|
type S = "a" | "b";
|
||||||
|
type T = S[] | S;
|
||||||
|
|
||||||
|
var s: S;
|
||||||
|
var t: T;
|
||||||
|
var str: string;
|
||||||
|
|
||||||
|
////////////////
|
||||||
|
|
||||||
|
s = <S>t;
|
||||||
|
s = t as S;
|
||||||
|
|
||||||
|
s = <S>str;
|
||||||
|
s = str as S;
|
||||||
|
|
||||||
|
////////////////
|
||||||
|
|
||||||
|
t = <T>s;
|
||||||
|
t = s as T;
|
||||||
|
|
||||||
|
t = <T>str;
|
||||||
|
t = str as T;
|
||||||
|
|
||||||
|
////////////////
|
||||||
|
|
||||||
|
str = <string>s;
|
||||||
|
str = s as string;
|
||||||
|
|
||||||
|
str = <string>t;
|
||||||
|
str = t as string;
|
Loading…
Reference in a new issue