mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Adding new tests
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
type A1 = true | false;
|
||||
type A2 = false | true;
|
||||
|
||||
function f1() {
|
||||
var a: A1;
|
||||
var a: A2;
|
||||
var a: true | false;
|
||||
var a: false | true;
|
||||
}
|
||||
|
||||
function f2(a: true | false, b: boolean) {
|
||||
a = b;
|
||||
b = a;
|
||||
}
|
||||
|
||||
function f3(a: true | false, b: true | false) {
|
||||
var x = a || b;
|
||||
var x = a && b;
|
||||
var x = !a;
|
||||
}
|
||||
|
||||
function f4(t: true, f: false) {
|
||||
var x1 = t && f;
|
||||
var x2 = f && t;
|
||||
var x3 = t || f;
|
||||
var x4 = f || t;
|
||||
var x5 = !t;
|
||||
var x6 = !f;
|
||||
}
|
||||
|
||||
declare function g(x: true): string;
|
||||
declare function g(x: false): boolean;
|
||||
declare function g(x: boolean): number;
|
||||
|
||||
function f5(b: boolean) {
|
||||
var z1 = g(true);
|
||||
var z2 = g(false);
|
||||
var z3 = g(b);
|
||||
}
|
||||
|
||||
function assertNever(x: never): never {
|
||||
throw new Error("Unexpected value");
|
||||
}
|
||||
|
||||
function f10(x: true | false) {
|
||||
switch (x) {
|
||||
case true: return "true";
|
||||
case false: return "false";
|
||||
}
|
||||
}
|
||||
|
||||
function f11(x: true | false) {
|
||||
switch (x) {
|
||||
case true: return "true";
|
||||
case false: return "false";
|
||||
}
|
||||
return assertNever(x);
|
||||
}
|
||||
|
||||
function f12(x: true | false) {
|
||||
if (x) {
|
||||
x;
|
||||
}
|
||||
else {
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
function f13(x: true | false) {
|
||||
if (x === true) {
|
||||
x;
|
||||
}
|
||||
else {
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
type Item =
|
||||
{ kind: true, a: string } |
|
||||
{ kind: false, b: string };
|
||||
|
||||
function f20(x: Item) {
|
||||
switch (x.kind) {
|
||||
case true: return x.a;
|
||||
case false: return x.b;
|
||||
}
|
||||
}
|
||||
|
||||
function f21(x: Item) {
|
||||
switch (x.kind) {
|
||||
case true: return x.a;
|
||||
case false: return x.b;
|
||||
}
|
||||
return assertNever(x);
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
// @strictNullChecks: true
|
||||
|
||||
type A1 = true | false;
|
||||
type A2 = false | true;
|
||||
|
||||
function f1() {
|
||||
var a: A1;
|
||||
var a: A2;
|
||||
var a: true | false;
|
||||
var a: false | true;
|
||||
}
|
||||
|
||||
function f2(a: true | false, b: boolean) {
|
||||
a = b;
|
||||
b = a;
|
||||
}
|
||||
|
||||
function f3(a: true | false, b: true | false) {
|
||||
var x = a || b;
|
||||
var x = a && b;
|
||||
var x = !a;
|
||||
}
|
||||
|
||||
function f4(t: true, f: false) {
|
||||
var x1 = t && f;
|
||||
var x2 = f && t;
|
||||
var x3 = t || f;
|
||||
var x4 = f || t;
|
||||
var x5 = !t;
|
||||
var x6 = !f;
|
||||
}
|
||||
|
||||
declare function g(x: true): string;
|
||||
declare function g(x: false): boolean;
|
||||
declare function g(x: boolean): number;
|
||||
|
||||
function f5(b: boolean) {
|
||||
var z1 = g(true);
|
||||
var z2 = g(false);
|
||||
var z3 = g(b);
|
||||
}
|
||||
|
||||
function assertNever(x: never): never {
|
||||
throw new Error("Unexpected value");
|
||||
}
|
||||
|
||||
function f10(x: true | false) {
|
||||
switch (x) {
|
||||
case true: return "true";
|
||||
case false: return "false";
|
||||
}
|
||||
}
|
||||
|
||||
function f11(x: true | false) {
|
||||
switch (x) {
|
||||
case true: return "true";
|
||||
case false: return "false";
|
||||
}
|
||||
return assertNever(x);
|
||||
}
|
||||
|
||||
function f12(x: true | false) {
|
||||
if (x) {
|
||||
x;
|
||||
}
|
||||
else {
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
function f13(x: true | false) {
|
||||
if (x === true) {
|
||||
x;
|
||||
}
|
||||
else {
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
type Item =
|
||||
{ kind: true, a: string } |
|
||||
{ kind: false, b: string };
|
||||
|
||||
function f20(x: Item) {
|
||||
switch (x.kind) {
|
||||
case true: return x.a;
|
||||
case false: return x.b;
|
||||
}
|
||||
}
|
||||
|
||||
function f21(x: Item) {
|
||||
switch (x.kind) {
|
||||
case true: return x.a;
|
||||
case false: return x.b;
|
||||
}
|
||||
return assertNever(x);
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
const enum Choice { Unknown, Yes, No };
|
||||
|
||||
type YesNo = Choice.Yes | Choice.No;
|
||||
type NoYes = Choice.No | Choice.Yes;
|
||||
type UnknownYesNo = Choice.Unknown | Choice.Yes | Choice.No;
|
||||
|
||||
function f1() {
|
||||
var a: YesNo;
|
||||
var a: NoYes;
|
||||
var a: Choice.Yes | Choice.No;
|
||||
var a: Choice.No | Choice.Yes;
|
||||
}
|
||||
|
||||
function f2(a: YesNo, b: UnknownYesNo, c: Choice) {
|
||||
b = a;
|
||||
c = a;
|
||||
c = b;
|
||||
}
|
||||
|
||||
function f3(a: Choice.Yes, b: YesNo) {
|
||||
var x = a + b;
|
||||
var x = a - b;
|
||||
var x = a * b;
|
||||
var x = a / b;
|
||||
var x = a % b;
|
||||
var x = a | b;
|
||||
var x = a & b;
|
||||
var x = a ^ b;
|
||||
var x = -b;
|
||||
var x = ~b;
|
||||
var y = a == b;
|
||||
var y = a != b;
|
||||
var y = a === b;
|
||||
var y = a !== b;
|
||||
var y = a > b;
|
||||
var y = a < b;
|
||||
var y = a >= b;
|
||||
var y = a <= b;
|
||||
var y = !b;
|
||||
}
|
||||
|
||||
function f4(a: Choice.Yes, b: YesNo) {
|
||||
a++;
|
||||
b++;
|
||||
}
|
||||
|
||||
declare function g(x: Choice.Yes): string;
|
||||
declare function g(x: Choice.No): boolean;
|
||||
declare function g(x: Choice): number;
|
||||
|
||||
function f5(a: YesNo, b: UnknownYesNo, c: Choice) {
|
||||
var z1 = g(Choice.Yes);
|
||||
var z2 = g(Choice.No);
|
||||
var z3 = g(a);
|
||||
var z4 = g(b);
|
||||
var z5 = g(c);
|
||||
}
|
||||
|
||||
function assertNever(x: never): never {
|
||||
throw new Error("Unexpected value");
|
||||
}
|
||||
|
||||
function f10(x: YesNo) {
|
||||
switch (x) {
|
||||
case Choice.Yes: return "true";
|
||||
case Choice.No: return "false";
|
||||
}
|
||||
}
|
||||
|
||||
function f11(x: YesNo) {
|
||||
switch (x) {
|
||||
case Choice.Yes: return "true";
|
||||
case Choice.No: return "false";
|
||||
}
|
||||
return assertNever(x);
|
||||
}
|
||||
|
||||
function f12(x: UnknownYesNo) {
|
||||
if (x) {
|
||||
x;
|
||||
}
|
||||
else {
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
function f13(x: UnknownYesNo) {
|
||||
if (x === Choice.Yes) {
|
||||
x;
|
||||
}
|
||||
else {
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
type Item =
|
||||
{ kind: Choice.Yes, a: string } |
|
||||
{ kind: Choice.No, b: string };
|
||||
|
||||
function f20(x: Item) {
|
||||
switch (x.kind) {
|
||||
case Choice.Yes: return x.a;
|
||||
case Choice.No: return x.b;
|
||||
}
|
||||
}
|
||||
|
||||
function f21(x: Item) {
|
||||
switch (x.kind) {
|
||||
case Choice.Yes: return x.a;
|
||||
case Choice.No: return x.b;
|
||||
}
|
||||
return assertNever(x);
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
// @strictNullChecks: true
|
||||
|
||||
const enum Choice { Unknown, Yes, No };
|
||||
|
||||
type YesNo = Choice.Yes | Choice.No;
|
||||
type NoYes = Choice.No | Choice.Yes;
|
||||
type UnknownYesNo = Choice.Unknown | Choice.Yes | Choice.No;
|
||||
|
||||
function f1() {
|
||||
var a: YesNo;
|
||||
var a: NoYes;
|
||||
var a: Choice.Yes | Choice.No;
|
||||
var a: Choice.No | Choice.Yes;
|
||||
}
|
||||
|
||||
function f2(a: YesNo, b: UnknownYesNo, c: Choice) {
|
||||
b = a;
|
||||
c = a;
|
||||
c = b;
|
||||
}
|
||||
|
||||
function f3(a: Choice.Yes, b: UnknownYesNo) {
|
||||
var x = a + b;
|
||||
var x = a - b;
|
||||
var x = a * b;
|
||||
var x = a / b;
|
||||
var x = a % b;
|
||||
var x = a | b;
|
||||
var x = a & b;
|
||||
var x = a ^ b;
|
||||
var x = -b;
|
||||
var x = ~b;
|
||||
var y = a == b;
|
||||
var y = a != b;
|
||||
var y = a === b;
|
||||
var y = a !== b;
|
||||
var y = a > b;
|
||||
var y = a < b;
|
||||
var y = a >= b;
|
||||
var y = a <= b;
|
||||
var y = !b;
|
||||
}
|
||||
|
||||
function f4(a: Choice.Yes, b: UnknownYesNo) {
|
||||
a++;
|
||||
b++;
|
||||
}
|
||||
|
||||
declare function g(x: Choice.Yes): string;
|
||||
declare function g(x: Choice.No): boolean;
|
||||
declare function g(x: Choice): number;
|
||||
|
||||
function f5(a: YesNo, b: UnknownYesNo, c: Choice) {
|
||||
var z1 = g(Choice.Yes);
|
||||
var z2 = g(Choice.No);
|
||||
var z3 = g(a);
|
||||
var z4 = g(b);
|
||||
var z5 = g(c);
|
||||
}
|
||||
|
||||
function assertNever(x: never): never {
|
||||
throw new Error("Unexpected value");
|
||||
}
|
||||
|
||||
function f10(x: YesNo) {
|
||||
switch (x) {
|
||||
case Choice.Yes: return "true";
|
||||
case Choice.No: return "false";
|
||||
}
|
||||
}
|
||||
|
||||
function f11(x: YesNo) {
|
||||
switch (x) {
|
||||
case Choice.Yes: return "true";
|
||||
case Choice.No: return "false";
|
||||
}
|
||||
return assertNever(x);
|
||||
}
|
||||
|
||||
function f12(x: UnknownYesNo) {
|
||||
if (x) {
|
||||
x;
|
||||
}
|
||||
else {
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
function f13(x: UnknownYesNo) {
|
||||
if (x === Choice.Yes) {
|
||||
x;
|
||||
}
|
||||
else {
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
type Item =
|
||||
{ kind: Choice.Yes, a: string } |
|
||||
{ kind: Choice.No, b: string };
|
||||
|
||||
function f20(x: Item) {
|
||||
switch (x.kind) {
|
||||
case Choice.Yes: return x.a;
|
||||
case Choice.No: return x.b;
|
||||
}
|
||||
}
|
||||
|
||||
function f21(x: Item) {
|
||||
switch (x.kind) {
|
||||
case Choice.Yes: return x.a;
|
||||
case Choice.No: return x.b;
|
||||
}
|
||||
return assertNever(x);
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
const enum Choice { Unknown, Yes, No };
|
||||
|
||||
type Yes = Choice.Yes;
|
||||
type YesNo = Choice.Yes | Choice.No;
|
||||
type NoYes = Choice.No | Choice.Yes;
|
||||
type UnknownYesNo = Choice.Unknown | Choice.Yes | Choice.No;
|
||||
|
||||
function f1(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) {
|
||||
a = a;
|
||||
a = b;
|
||||
a = c;
|
||||
a = d;
|
||||
}
|
||||
|
||||
function f2(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) {
|
||||
b = a;
|
||||
b = b;
|
||||
b = c;
|
||||
b = d;
|
||||
}
|
||||
|
||||
function f3(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) {
|
||||
c = a;
|
||||
c = b;
|
||||
c = c;
|
||||
c = d;
|
||||
}
|
||||
|
||||
function f4(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) {
|
||||
d = a;
|
||||
d = b;
|
||||
d = c;
|
||||
d = d;
|
||||
}
|
||||
|
||||
function f5(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) {
|
||||
a = Choice.Unknown;
|
||||
a = Choice.Yes;
|
||||
a = Choice.No;
|
||||
b = Choice.Unknown;
|
||||
b = Choice.Yes;
|
||||
b = Choice.No;
|
||||
c = Choice.Unknown;
|
||||
c = Choice.Yes;
|
||||
c = Choice.No;
|
||||
d = Choice.Unknown;
|
||||
d = Choice.Yes;
|
||||
d = Choice.No;
|
||||
}
|
||||
|
||||
function f6(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) {
|
||||
a === Choice.Unknown;
|
||||
a === Choice.Yes;
|
||||
a === Choice.No;
|
||||
b === Choice.Unknown;
|
||||
b === Choice.Yes;
|
||||
b === Choice.No;
|
||||
c === Choice.Unknown;
|
||||
c === Choice.Yes;
|
||||
c === Choice.No;
|
||||
d === Choice.Unknown;
|
||||
d === Choice.Yes;
|
||||
d === Choice.No;
|
||||
}
|
||||
|
||||
function f7(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) {
|
||||
a === a;
|
||||
a === b;
|
||||
a === c;
|
||||
a === d;
|
||||
b === a;
|
||||
b === b;
|
||||
b === c;
|
||||
b === d;
|
||||
c === a;
|
||||
c === b;
|
||||
c === c;
|
||||
c === d;
|
||||
d === a;
|
||||
d === b;
|
||||
d === c;
|
||||
d === d;
|
||||
}
|
||||
|
||||
function f10(x: Yes): Yes {
|
||||
switch (x) {
|
||||
case Choice.Unknown: return x;
|
||||
case Choice.Yes: return x;
|
||||
case Choice.No: return x;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
function f11(x: YesNo): YesNo {
|
||||
switch (x) {
|
||||
case Choice.Unknown: return x;
|
||||
case Choice.Yes: return x;
|
||||
case Choice.No: return x;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
function f12(x: UnknownYesNo): UnknownYesNo {
|
||||
switch (x) {
|
||||
case Choice.Unknown: return x;
|
||||
case Choice.Yes: return x;
|
||||
case Choice.No: return x;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
function f13(x: Choice): Choice {
|
||||
switch (x) {
|
||||
case Choice.Unknown: return x;
|
||||
case Choice.Yes: return x;
|
||||
case Choice.No: return x;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
// @strictNullChecks: true
|
||||
|
||||
let zero: 0 = 0;
|
||||
let one: 1 = 1;
|
||||
let two: 2 = 2;
|
||||
let oneOrTwo: 1 | 2 = <1 | 2>1;
|
||||
|
||||
function f1(x: 0 | 1 | 2) {
|
||||
switch (x) {
|
||||
case zero:
|
||||
x;
|
||||
break;
|
||||
case one:
|
||||
x;
|
||||
break;
|
||||
case two:
|
||||
x;
|
||||
break;
|
||||
default:
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
function f2(x: 0 | 1 | 2) {
|
||||
switch (x) {
|
||||
case zero:
|
||||
x;
|
||||
break;
|
||||
case oneOrTwo:
|
||||
x;
|
||||
break;
|
||||
default:
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
type Falsy = false | 0 | "" | null | undefined;
|
||||
|
||||
function f3(x: Falsy) {
|
||||
if (x) {
|
||||
x;
|
||||
}
|
||||
else {
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
function f4(x: 0 | 1 | true | string) {
|
||||
switch (x) {
|
||||
case 0:
|
||||
x;
|
||||
break;
|
||||
case 1:
|
||||
x;
|
||||
break;
|
||||
case "abc":
|
||||
case "def":
|
||||
x;
|
||||
break;
|
||||
case null:
|
||||
x;
|
||||
break;
|
||||
case undefined:
|
||||
x;
|
||||
break;
|
||||
default:
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
function f5(x: string | number | boolean) {
|
||||
switch (x) {
|
||||
case "abc":
|
||||
x;
|
||||
break;
|
||||
case 0:
|
||||
case 1:
|
||||
x;
|
||||
break;
|
||||
case true:
|
||||
x;
|
||||
break;
|
||||
case "hello":
|
||||
case 123:
|
||||
x;
|
||||
break;
|
||||
default:
|
||||
x;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
type A1 = 1;
|
||||
type A2 = 1.0;
|
||||
type A3 = 1e0;
|
||||
type A4 = 10e-1;
|
||||
type A5 = 1 | 1.0 | 1e0 | 10e-1;
|
||||
|
||||
function f1() {
|
||||
var a: A1 = 1;
|
||||
var a: A2 = 1;
|
||||
var a: A3 = 1;
|
||||
var a: A4 = 1;
|
||||
var a: A5 = 1;
|
||||
}
|
||||
|
||||
type B1 = -1 | 0 | 1;
|
||||
type B2 = 1 | 0 | -1;
|
||||
type B3 = 0 | -1 | 1;
|
||||
|
||||
function f2() {
|
||||
var b: B1 = -1;
|
||||
var b: B2 = 0;
|
||||
var b: B3 = 1;
|
||||
}
|
||||
|
||||
function f3(a: 1, b: 0 | 1 | 2) {
|
||||
var x = a + b;
|
||||
var x = a - b;
|
||||
var x = a * b;
|
||||
var x = a / b;
|
||||
var x = a % b;
|
||||
var x = a | b;
|
||||
var x = a & b;
|
||||
var x = a ^ b;
|
||||
var x = -b;
|
||||
var x = ~b;
|
||||
var y = a == b;
|
||||
var y = a != b;
|
||||
var y = a === b;
|
||||
var y = a !== b;
|
||||
var y = a > b;
|
||||
var y = a < b;
|
||||
var y = a >= b;
|
||||
var y = a <= b;
|
||||
var y = !b;
|
||||
}
|
||||
|
||||
function f4(a: 1, b: 0 | 1 | 2) {
|
||||
a++;
|
||||
b++;
|
||||
}
|
||||
|
||||
declare function g(x: 0): string;
|
||||
declare function g(x: 1): boolean;
|
||||
declare function g(x: number): number;
|
||||
|
||||
function f5(a: 1, b: 0 | 1 | 2) {
|
||||
var z1 = g(0);
|
||||
var z2 = g(1);
|
||||
var z3 = g(2);
|
||||
var z4 = g(a);
|
||||
var z5 = g(b);
|
||||
}
|
||||
|
||||
function assertNever(x: never): never {
|
||||
throw new Error("Unexpected value");
|
||||
}
|
||||
|
||||
type Tag = 0 | 1 | 2;
|
||||
|
||||
function f10(x: Tag) {
|
||||
switch (x) {
|
||||
case 0: return "a";
|
||||
case 1: return "b";
|
||||
case 2: return "c";
|
||||
}
|
||||
}
|
||||
|
||||
function f11(x: Tag) {
|
||||
switch (x) {
|
||||
case 0: return "a";
|
||||
case 1: return "b";
|
||||
case 2: return "c";
|
||||
}
|
||||
return assertNever(x);
|
||||
}
|
||||
|
||||
function f12(x: Tag) {
|
||||
if (x) {
|
||||
x;
|
||||
}
|
||||
else {
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
function f13(x: Tag) {
|
||||
if (x === 0 || x === 2) {
|
||||
x;
|
||||
}
|
||||
else {
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
function f14(x: 0 | 1 | 2, y: string) {
|
||||
var a = x && y;
|
||||
var b = x || y;
|
||||
}
|
||||
|
||||
function f15(x: 0 | false, y: 1 | "one") {
|
||||
var a = x && y;
|
||||
var b = y && x;
|
||||
var c = x || y;
|
||||
var d = y || x;
|
||||
var e = !x;
|
||||
var f = !y;
|
||||
}
|
||||
|
||||
type Item =
|
||||
{ kind: 0, a: string } |
|
||||
{ kind: 1, b: string } |
|
||||
{ kind: 2, c: string };
|
||||
|
||||
function f20(x: Item) {
|
||||
switch (x.kind) {
|
||||
case 0: return x.a;
|
||||
case 1: return x.b;
|
||||
case 2: return x.c;
|
||||
}
|
||||
}
|
||||
|
||||
function f21(x: Item) {
|
||||
switch (x.kind) {
|
||||
case 0: return x.a;
|
||||
case 1: return x.b;
|
||||
case 2: return x.c;
|
||||
}
|
||||
return assertNever(x);
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
// @strictNullChecks: true
|
||||
|
||||
type A1 = 1;
|
||||
type A2 = 1.0;
|
||||
type A3 = 1e0;
|
||||
type A4 = 10e-1;
|
||||
type A5 = 1 | 1.0 | 1e0 | 10e-1;
|
||||
|
||||
function f1() {
|
||||
var a: A1 = 1;
|
||||
var a: A2 = 1;
|
||||
var a: A3 = 1;
|
||||
var a: A4 = 1;
|
||||
var a: A5 = 1;
|
||||
}
|
||||
|
||||
type B1 = -1 | 0 | 1;
|
||||
type B2 = 1 | 0 | -1;
|
||||
type B3 = 0 | -1 | 1;
|
||||
|
||||
function f2() {
|
||||
var b: B1 = -1;
|
||||
var b: B2 = 0;
|
||||
var b: B3 = 1;
|
||||
}
|
||||
|
||||
function f3(a: 1, b: 0 | 1 | 2) {
|
||||
var x = a + b;
|
||||
var x = a - b;
|
||||
var x = a * b;
|
||||
var x = a / b;
|
||||
var x = a % b;
|
||||
var x = a | b;
|
||||
var x = a & b;
|
||||
var x = a ^ b;
|
||||
var x = -b;
|
||||
var x = ~b;
|
||||
var y = a == b;
|
||||
var y = a != b;
|
||||
var y = a === b;
|
||||
var y = a !== b;
|
||||
var y = a > b;
|
||||
var y = a < b;
|
||||
var y = a >= b;
|
||||
var y = a <= b;
|
||||
var y = !b;
|
||||
}
|
||||
|
||||
function f4(a: 1, b: 0 | 1 | 2) {
|
||||
a++;
|
||||
b++;
|
||||
}
|
||||
|
||||
declare function g(x: 0): string;
|
||||
declare function g(x: 1): boolean;
|
||||
declare function g(x: number): number;
|
||||
|
||||
function f5(a: 1, b: 0 | 1 | 2) {
|
||||
var z1 = g(0);
|
||||
var z2 = g(1);
|
||||
var z3 = g(2);
|
||||
var z4 = g(a);
|
||||
var z5 = g(b);
|
||||
}
|
||||
|
||||
function assertNever(x: never): never {
|
||||
throw new Error("Unexpected value");
|
||||
}
|
||||
|
||||
type Tag = 0 | 1 | 2;
|
||||
|
||||
function f10(x: Tag) {
|
||||
switch (x) {
|
||||
case 0: return "a";
|
||||
case 1: return "b";
|
||||
case 2: return "c";
|
||||
}
|
||||
}
|
||||
|
||||
function f11(x: Tag) {
|
||||
switch (x) {
|
||||
case 0: return "a";
|
||||
case 1: return "b";
|
||||
case 2: return "c";
|
||||
}
|
||||
return assertNever(x);
|
||||
}
|
||||
|
||||
function f12(x: Tag) {
|
||||
if (x) {
|
||||
x;
|
||||
}
|
||||
else {
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
function f13(x: Tag) {
|
||||
if (x === 0 || x === 2) {
|
||||
x;
|
||||
}
|
||||
else {
|
||||
x;
|
||||
}
|
||||
}
|
||||
|
||||
function f14(x: 0 | 1 | 2, y: string) {
|
||||
var a = x && y;
|
||||
var b = x || y;
|
||||
}
|
||||
|
||||
function f15(x: 0 | false, y: 1 | "one") {
|
||||
var a = x && y;
|
||||
var b = y && x;
|
||||
var c = x || y;
|
||||
var d = y || x;
|
||||
var e = !x;
|
||||
var f = !y;
|
||||
}
|
||||
|
||||
type Item =
|
||||
{ kind: 0, a: string } |
|
||||
{ kind: 1, b: string } |
|
||||
{ kind: 2, c: string };
|
||||
|
||||
function f20(x: Item) {
|
||||
switch (x.kind) {
|
||||
case 0: return x.a;
|
||||
case 1: return x.b;
|
||||
case 2: return x.c;
|
||||
}
|
||||
}
|
||||
|
||||
function f21(x: Item) {
|
||||
switch (x.kind) {
|
||||
case 0: return x.a;
|
||||
case 1: return x.b;
|
||||
case 2: return x.c;
|
||||
}
|
||||
return assertNever(x);
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
type A = 1;
|
||||
type B = 2 | 3;
|
||||
type C = 1 | 2 | 3;
|
||||
type D = 0 | 1 | 2;
|
||||
|
||||
function f1(a: A, b: B, c: C, d: D) {
|
||||
a = a;
|
||||
a = b;
|
||||
a = c;
|
||||
a = d;
|
||||
}
|
||||
|
||||
function f2(a: A, b: B, c: C, d: D) {
|
||||
b = a;
|
||||
b = b;
|
||||
b = c;
|
||||
b = d;
|
||||
}
|
||||
|
||||
function f3(a: A, b: B, c: C, d: D) {
|
||||
c = a;
|
||||
c = b;
|
||||
c = c;
|
||||
c = d;
|
||||
}
|
||||
|
||||
function f4(a: A, b: B, c: C, d: D) {
|
||||
d = a;
|
||||
d = b;
|
||||
d = c;
|
||||
d = d;
|
||||
}
|
||||
|
||||
function f5(a: A, b: B, c: C, d: D) {
|
||||
a = 0;
|
||||
a = 1;
|
||||
a = 2;
|
||||
a = 3;
|
||||
b = 0;
|
||||
b = 1;
|
||||
b = 2;
|
||||
b = 3;
|
||||
c = 0;
|
||||
c = 1;
|
||||
c = 2;
|
||||
c = 3;
|
||||
d = 0;
|
||||
d = 1;
|
||||
d = 2;
|
||||
d = 3;
|
||||
}
|
||||
|
||||
function f6(a: A, b: B, c: C, d: D) {
|
||||
a === 0;
|
||||
a === 1;
|
||||
a === 2;
|
||||
a === 3;
|
||||
b === 0;
|
||||
b === 1;
|
||||
b === 2;
|
||||
b === 3;
|
||||
c === 0;
|
||||
c === 1;
|
||||
c === 2;
|
||||
c === 3;
|
||||
d === 0;
|
||||
d === 1;
|
||||
d === 2;
|
||||
d === 3;
|
||||
}
|
||||
|
||||
function f7(a: A, b: B, c: C, d: D) {
|
||||
a === a;
|
||||
a === b;
|
||||
a === c;
|
||||
a === d;
|
||||
b === a;
|
||||
b === b;
|
||||
b === c;
|
||||
b === d;
|
||||
c === a;
|
||||
c === b;
|
||||
c === c;
|
||||
c === d;
|
||||
d === a;
|
||||
d === b;
|
||||
d === c;
|
||||
d === d;
|
||||
}
|
||||
|
||||
function f8(x: 0 | 2 | 4) {
|
||||
switch (x) {
|
||||
case 0: return;
|
||||
case 1: return;
|
||||
case 2: return;
|
||||
case 3: return;
|
||||
case 4: return;
|
||||
case 5: return;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user