10x Developer
  • Introduction
  • HTML
    • HTML DOM Jquery
    • HTML: Form
    • CSS
    • CSS Layout
    • CSS Flexbox
    • CSS Grid
    • SASS/LESS
    • CSS/LESS/SASS Cookbook
    • Bootstrap
  • JavaScript
    • JavaScript
    • ES6
    • TypeScript
    • JavaScript Testing
    • JavaScript Event Loop
    • DOM
    • Web APIs
    • JSON
    • RegEx
    • Functional Programming
    • JavaScript Lib
    • CoffeeScript
    • CoffeeScript to ES6 cheatsheet
  • Angular.js
    • Angular
    • Angular Cookbook
    • Angular Mistakes I Made
    • Angular 1.x
  • React.js
    • React.js
  • Node
    • Node.js
    • CLI command line tool
    • Electron / Atom
    • NW.js (node-webkit)
  • Serverless
    • AWS Lambda
    • Google Cloud Function
    • Actions on Google / Google Assistant
  • Full Stack Development
    • HTTP
    • Meteor
    • MongoDB
    • Digital Ocean
    • UI
    • Sketch
    • Web Dev Resources
  • Lang
    • Ruby
  • Know Your Tools
    • Chrome DevTools
    • Editor: VS Code
    • Editor: Vim
    • Editor: Sublime
    • Editor: Atom
    • Windows
    • Git
    • Linux / Bash
    • Mac
  • Cheatsheets
Powered by GitBook
On this page
  1. JavaScript

TypeScript

PreviousES6NextJavaScript Testing

Last updated 7 years ago

CtrlK
  • References
  • Tools
  • TypeScript Notes/Tips/Patterns
  • Favor getter over method in TS
  • Type: How to declare type to object property and key?
  • Type: type of array item based on index
  • Use Destructuring (FP concept)
  • Be careful: it's too easy to create private member

References

  • Learn TypeScript in Y Minutes

  • TypeScript Deep Dive

Tools

  • A minimal Yeoman Generator for creating NodeJS modules using TypeScript

TypeScript Notes/Tips/Patterns

  • Scope: by default members in a class are public

  • Enum: an enum is a way of giving more friendly names to sets of numeric values

    • String enum: use custom Type. See more here.

  • Do we put return type when defining function? Yes. getSum(a: number, b: number): number {}

  • Type assertion: (<string>someValue).length;

  • We have static, readonly

    • const is for variable. No one can modify it after assigned.

    • readonly is for property (class property to property in interface). Properties must be initialized

  • Function overload, this is possible in TypeScript but it's not supported by JavaScript.

  • Default parameter: getSum(data: number[], skipNegative: boolean = false)

Favor getter over method in TS

Type: How to declare type to object property and key?

var stuff: { [key: string]: string; } = {};
stuff['a'] = ''; // ok
stuff['a'] = 4;  // error

Type: type of array item based on index

  • public label(row: {0: string, 1: number}): string {

  • var t:[number, string] = [1, "message"]

Use Destructuring (FP concept)

// pick only the data we need using destructing
// Function signature: getAccountInfo(): {balance: number; cardStatus: string; cardNumber: string; cardHolder: string}

// before:
.then((response:any) => {
        const data = this.getAccountInfo()
        return {
          balance: data.balance,
          cardStatus: data.cardStatus
        };

// after
.then((response:any) => {
        const {balance, cardStatus} = this.getAccountInfo()
        return {
          balance,
          cardStatus
        };

Be careful: it's too easy to create private member

If the data is NOT used anywhere, don't put private.

constructor(private data?: any) {
      this.model = this.extract(data);
    }
at their declaration or in the constructor
. It only ensures that it won't be modified by me. Others can still modify it.
  • static must be class member.

  • See here