mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Added tests.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
// @declaration: true
|
||||
|
||||
/**
|
||||
* A makeshift string enum.
|
||||
*/
|
||||
namespace EventType {
|
||||
export type Click = string & { _fooTag: any };
|
||||
export const Click = <Click>"Click";
|
||||
|
||||
export type KeyDown = string & { _barTag: any };
|
||||
export const KeyDown = <KeyDown>"KeyDown";
|
||||
}
|
||||
|
||||
/**
|
||||
* The all-encompassing type for our makeshift enum.
|
||||
*/
|
||||
type EventType = EventType.Click
|
||||
| EventType.KeyDown;
|
||||
|
||||
|
||||
interface BaseEvent {
|
||||
type: EventType;
|
||||
}
|
||||
|
||||
interface ClickEvent extends BaseEvent {
|
||||
type: EventType.Click;
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
interface KeyDownEvent extends BaseEvent {
|
||||
type: EventType.KeyDown;
|
||||
keyCode: number;
|
||||
}
|
||||
|
||||
function isActionType(action: BaseEvent, type: EventType.Click): action is ClickEvent;
|
||||
function isActionType(action: BaseEvent, type: EventType.KeyDown): action is KeyDownEvent;
|
||||
function isActionType(action: BaseEvent, type: EventType): action is BaseEvent;
|
||||
function isActionType(action: BaseEvent, type: EventType) {
|
||||
return action.type === type;
|
||||
}
|
||||
|
||||
let handleAction = (action: BaseEvent) => {
|
||||
if (isActionType(action, EventType.Click)) {
|
||||
let foo = action.x;
|
||||
let bar = action.y;
|
||||
}
|
||||
|
||||
if (isActionType(action, EventType.KeyDown)) {
|
||||
let bar = action.keyCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// @declaration: true
|
||||
|
||||
interface Nil {
|
||||
}
|
||||
|
||||
interface Cons<T> {
|
||||
value: T;
|
||||
next: List<T>;
|
||||
}
|
||||
|
||||
type List<T> = Cons<T> | Nil;
|
||||
const nil: Nil = {};
|
||||
function cons<T>(value: T, next: List<T>) {
|
||||
return { value, next };
|
||||
}
|
||||
|
||||
function hasElements<T>(list: Cons<T>): list is Cons<T>;
|
||||
function hasElements<T>(list: List<T>): list is Cons<T>;
|
||||
function hasElements<T>(list: List<T>): list is Cons<T> {
|
||||
return !!(list as Cons<T>).next;
|
||||
}
|
||||
|
||||
function isEmpty(list: Nil): list is Nil;
|
||||
function isEmpty<T>(list: List<T>): list is Nil;
|
||||
function isEmpty<T>(list: List<T>): boolean {
|
||||
return !isEmpty(list);
|
||||
}
|
||||
|
||||
let listA = cons(1, cons(2, cons(3, nil)));
|
||||
let listB = nil;
|
||||
let listC: List<number> = listA || listB;
|
||||
|
||||
if (isEmpty(listA)) {
|
||||
let a = listA;
|
||||
}
|
||||
else {
|
||||
let a = listA;
|
||||
}
|
||||
|
||||
if (hasElements(listC)) {
|
||||
let { value } = listC;
|
||||
}
|
||||
else {
|
||||
let myNil: Nil = listC;
|
||||
}
|
||||
|
||||
if (hasElements(listB)) {
|
||||
let somehowCons: Cons<any> = listB;
|
||||
}
|
||||
else {
|
||||
let myNil: Nil = listB;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// @declaration: true
|
||||
|
||||
function is(x: any, type: "number"): x is number;
|
||||
function is(x: any, type: "string"): x is string;
|
||||
function is(x: any, type: "boolean"): x is boolean;
|
||||
function is(x: any, type: string): x is (number | boolean | string);
|
||||
function is(x: any, type: string): boolean {
|
||||
if (["string", "number", "boolean"].indexOf(type) >= 0) {
|
||||
return typeof x === type;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
declare function myRand(): boolean;
|
||||
|
||||
let strNumOrBool: string | number | boolean;
|
||||
|
||||
if (myRand()) {
|
||||
strNumOrBool = "abc";
|
||||
}
|
||||
else if (myRand()) {
|
||||
strNumOrBool = 100;
|
||||
}
|
||||
else {
|
||||
strNumOrBool = true;
|
||||
}
|
||||
|
||||
if (is(strNumOrBool, "number")) {
|
||||
let num = strNumOrBool;
|
||||
num *= 100;
|
||||
}
|
||||
|
||||
if (is(strNumOrBool, "string")) {
|
||||
let str = strNumOrBool;
|
||||
str = str.slice();
|
||||
}
|
||||
|
||||
if (is(strNumOrBool, "boolean")) {
|
||||
let bool = strNumOrBool;
|
||||
bool = bool || bool && bool;
|
||||
}
|
||||
Reference in New Issue
Block a user