8000 Added tests. · sweshgit/TypeScript@6b1c91b · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b1c91b

Browse files
Added tests.
1 parent 90273f9 commit 6b1c91b

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// @declaration: true
2+
3+
/**
4+
* A makeshift string enum.
5+
*/
6+
namespace EventType {
7+
export type Click = string & { _fooTag: any };
8+
export const Click = <Click>"Click";
9+
10+
export type KeyDown = string & { _barTag: any };
11+
export const KeyDown = <KeyDown>"KeyDown";
12+
}
13+
14+
/**
15+
* The all-encompassing type for our makeshift enum.
16+
*/
17+
type EventType = EventType.Click
18+
| EventType.KeyDown;
19+
20+
21+
interface BaseEvent {
22+
type: EventType;
23+
}
24+
25+
interface ClickEvent extends BaseEvent {
26+
type: EventType.Click;
27+
x: number;
28+
y: number;
29+
}
30+
31+
interface KeyDownEvent extends BaseEvent {
32+
type: EventType.KeyDown;
33+
keyCode: number;
34+
}
35+
36+
function isActionType(action: BaseEvent, type: EventType.Click): action is ClickEvent;
37+
function isActionType(action: BaseEvent, type: EventType.KeyDown): action is KeyDownEvent;
38+
function isActionType(action: BaseEvent, type: EventType): action is BaseEvent;
39+
function isActionType(action: BaseEvent, type: EventType) {
40+
return action.type === type;
41+
}
42+
43+
let handleAction = (action: BaseEvent) => {
44+
if (isActionType(action, EventType.Click)) {
45+
let foo = action.x;
46+
let bar = action.y;
47+
}
48+
49+
if (isActionType(action, EventType.KeyDown)) {
50+
let bar = action.keyCode;
51+
}
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// @declaration: true
2+
3+
interface Nil {
4+
}
5+
6+
interface Cons<T> {
7+
value: T;
8+
next: List<T>;
9+
}
10+
11+
type List<T> = Cons<T> | Nil;
12+
const nil: Nil = {};
13+
function cons<T>(value: T, next: List<T>) {
14+
return { value, next };
15+
}
16+
17 EDBE +
function hasElements<T>(list: Cons<T>): list is Cons<T>;
18+
function hasElements<T>(list: List<T>): list is Cons<T>;
19+
function hasElements<T>(list: List<T>): list is Cons<T> {
20+
return !!(list as Cons<T>).next;
21+
}
22+
23+
function isEmpty(list: Nil): list is Nil;
24+
function isEmpty<T>(list: List<T>): list is Nil;
25+
function isEmpty<T>(list: List<T>): boolean {
26+
return !isEmpty(list);
27+
}
28+
29+
let listA = cons(1, cons(2, cons(3, nil)));
30+
let listB = nil;
31+
let listC: List<number> = listA || listB;
32+
33+
if (isEmpty(listA)) {
34+
let a = listA;
35+
}
36+
else {
37+
let a = listA;
38+
}
39+
40+
if (hasElements(listC)) {
41+
let { value } = listC;
42+
}
43+
else {
44+
let myNil: Nil = listC;
45+
}
46+
47+
if (hasElements(listB)) {
48+
let somehowCons: Cons<any> = listB;
49+
}
50+
else {
51+
let myNil: Nil = listB;
52+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// @declaration: true
2+
3+
function is(x: any, type: "number"): x is number;
4+
function is(x: any, type: "string"): x is string;
5+
function is(x: any, type: "boolean"): x is boolean;
6+
function is(x: any, type: string): x is (number | boolean | string);
7+
function is(x: any, type: string): boolean {
8+
if (["string", "number", "boolean"].indexOf(type) >= 0) {
9+
return typeof x === type;
10+
}
11+
12+
return false;
13+
}
14+
15+
declare function myRand(): boolean;
16+
17+
let strNumOrBool: string | number | boolean;
18+
19+
if (myRand()) {
20+
strNumOrBool = "abc";
21+
}
22+
else if (myRand()) {
23+
strNumOrBool = 100;
24+
}
25+
else {
26+
strNumOrBool = true;
27+
}
28+
29+
if (is(strNumOrBool, "number")) {
30+
let num = strNumOrBool;
31+
num *= 100;
32+
}
33+
34+
if (is(strNumOrBool, "string")) {
35+
let str = strNumOrBool;
36+
str = str.slice();
37+
}
38+
39+
if (is(strNumOrBool, "boolean")) {
40+
let bool = strNumOrBool;
41+
bool = bool || bool && bool;
42+
}

0 commit comments

Comments
 (0)
0