Coder Social home page Coder Social logo

typescript2-cheatsheet's Introduction

Typescript2-CheatSheet

My personal cheat sheet for Typescript 2

Exports

  • everything can be exported
    export interface StringValidator
    export const numberRegexp
    export class ZipCodeValidator 
  • exports can be renamed
export { ZipCodeValidator as mainValidator };
  • You can export something without importing it on your own
export {ZipCodeValidator as RegExpBasedZipCodeValidator} from "./ZipCodeValidator";

Imports

  • You can import a single export
import { ZipCodeValidator } from "./ZipCodeValidator";
  • you can rename an import
import { ZipCodeValidator as ZCV } from "./ZipCodeValidator";
  • you can import an entire module
import * as validator from "./ZipCodeValidator";
let myValidator = new validator.ZipCodeValidator();
  • take care of default export. Import is different and act as a rename. Below import num is the same thing as import default as num.
export default "123";

import num from "./OneTwoThree";
console.log(num); // "123"

Properties getter/setter

private _timeInterval: number;

get timeInterval(): number {
    return this._timeInterval;
}
 
set timeInterval(value: number) {
    if (value === undefined) throw 'Please supply time interval';
    this._timeInterval = value;
}

Instantiate interface

Instead of

this.model = Object(); // initialize this to an empty object

Do this :

this._models = <IModels> {};

Instantiate empty object and then add properties dynamically

    var opts:any = {}
    opts.jwtFromRequest = ExtractJwt.fromAuthHeader();
    opts.secretOrKey = 'config.secret';

Singleton


private static instance: MyClassName;

static getInstance() {
        if (!MyClassName.instance) {
            MyClassName.instance = new MyClassName();
            // ... any one time initialization goes here ...
        }
        return MyClassName.instance;
    }

Get/Set/accesors/property

get Text():string {

}

set Text(value:string) {
}

Tips for importing JS without having types for it

declare const foo: any;

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.