Code Monkey home page Code Monkey logo

typescript-cheatsheet's Introduction

Typescript Cheatsheet

2016-07-22

var VARIABLE_NAME : type = value

Types

  • boolean
  • number
  • string

Arrays

var list:number[] = [1,2,3];
var list:Array<number> = [1,2,3];

Enum

enum Color {Red, Green, Blue};
var c: Color = Color.Green;

Oop

Properties visibility

class Greeter {
	private greeting: string;
	constructor(message: string){
		this.greeting = message;
	}
	public greet() : string{
		return "Hello, " + this.greeting;
	}
}
var greeter:Greeter = new Greeter("world");

Inheritance

class Animal {
	name:string;
	contructor(theName:string){ this.name = theName; }
	move(meters: number=0){
		alert(this.name + " moved " + meters + "m.");
	}
}

class Snake extends Animal {
	name:string;
	contructor(name:string){  super(name); }
	move(meters=5){
		alert("Slithering...");
		super.move(meters);
	}
}

Accessors

class Person {
	private _password: string;

	get password(): string {
		return this._password;
	}

	set password(p: string){
		if(p != "123456"){
			this._password = p;
		}
		else{
			alert("Hey, the password can not be: 123456");
		}
	}
}

var p = new Person();
p.password = "123456"; // shows the error

Static, interfaces

class SystemAlert{
	static alert(message:string):void{
		alert(message);
	}
}

interface Point {
	x: number;
	y: number;
}

class Point3d implements Point{
	// ...
}

Optional parameter, rest parameters, json params

class Foo {
	buildName(firstName: string, lastName?: string){
		if(lastName){
			// ...	
		}
	}	
}

class Foo2 {
	static alertName(firstName: string, ...restOfName: string[]) {
		alert(firstName + " " + restOfName.join(" "));
	}
}

class Foo3 {
	constructor( p: {x:number; y:number; z?:number;} ){

	}
}

Decorators

import {Component} from 'angular2/core';

@Component({
	selector: 'my-app',
	template: '<h1>My First Angular 2 App</h1>',
})
export class AppComponent{ }

typescript-cheatsheet's People

Contributors

lingtalfi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.