Skip to content

Instantly share code, notes, and snippets.

@spion
Created November 9, 2015 18:31
Show Gist options
  • Select an option

  • Save spion/0f3cb8cd702aaada6c93 to your computer and use it in GitHub Desktop.

Select an option

Save spion/0f3cb8cd702aaada6c93 to your computer and use it in GitHub Desktop.

interfaces

Open in TypeScript playground

interface Item {
	name: string
	created:Date
}
declare function test(i:Item);


test({name: 'hello', created: new Date})

class MyItem {
	constructor(public name:string, public created:Date){}
}

test(new MyItem('hello', new Date))

guards

Open in TypeScript playground

declare function resolve<T>(t:T):PromiseLike<T>

function isPromise<T>(p:any): p is PromiseLike<T> {
	return typeof p && p.then === 'function'
}

function add5(p:number|PromiseLike<number>) {
	if (isPromise(p)) {
		return p.then(v => v + 5)
	} else {
		return resolve(p + 5)
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment