Skip to content

damminhtien/typescript.short

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 

Repository files navigation

Short and simple bullet points about Typescript 💛

1. Introduction

  • Typescript is the typed superset of Javascript that compiles (transpiles) to Javascript
  • Why Typescript rather writing in Javascript:
    • Typescript as its name adds type(s) enforcement which Javascript wont
    • Typescript has much neat easy to maintain
    • Typescript is cross platform
  • Hi world program:
function sayHi(name?: string = 'world'): string { 
    return 'Hi ' + name;
} 
console.log(sayHi('world'))

2. Types

  • number
const n: number = 100
  • string
const s: string = 'Hi world'
  • boolean
const b: boolean = true
  • any
const a: any = 💟
  • void
const v = (): void => {}
  • null
const a: null = null
  • enums
enum greeting = { hello, hi, xinchao }
  • Array
const greeting Array<string> = [ 'hello', 'hi', 'xinchao' ]
const fruit string[] = [ 🍏, 🍉,  🥝,  🍇,  🥑,  🥥 ]

3. Function

  • Rest parameters
function sayHiEveryOne(...names: Array<string>): string {
    return 'Hi ' + names.join(', ')
}
sayHiEveryOne('Hi ', 'Type', "Script"); // returns 'Hi Type Script'

4. Interfaces

interface Person {
  name: string
  age?: number
}

const ps: Person = { 
  name: 'damminhtien'
  age: 18
} 

interface SaySmt {
  (name: string): string
}

function sayHi(name: string): string {
  return 'Hi ' + name
}

const sayHiWorld: SaySmt = sayHi

5. Class

  • Simple class
class Person {
  readonly name: string
  age?: number

  constructor(name: string, age?: number) {
    this.name = name
    this.age = age
  }

  getAge(): number {
    return this.age
  }
}
  • Abstract class

Note: The class which implements an abstract class must call super() in the constructor

class Person {
  abstract name: number
  age: string

  abstract getAge(): number
}

class Lover extends Person {
  name: string
  age: number
  
  constructor() {
  }
}
  • Data modifiers

    • public

    By default, all members of a class in TypeScript are public. All the public members can be accessed anywhere without any restrictions.

    class Dog {
      name: string
      public color: string
    }
    let dog = new Dog()
    dog.name = 'Ki'
    dog.color = 'white'
    • private

    The private access modifier ensures that class members are visible only to that class and are not accessible outside the containing class.

    class Dog {
      name: string
      private color: string
    }
    let dog = new Dog()
    dog.name = 'Ki'
    dog.color = 'white' // error 
    • protected

    The protected access modifier is similar to the private access modifier, except that protected members can be accessed using their deriving classes.

    class Animal {
      protected name: string
      constructor(name: string) {
        this.name = name
      }
    }
    class Dog extends Animal{
      public color: string
      constructor(name: string, color: string) {
        super(name)
        this.color = color
      }
    }
    let dog = new Dog()
    dog.name = 'Ki' // error
    dog.color = 'white'
  • static

class Color {
  static BLUE: number = '#0000FF'
  
  static getBlue(): string {
    return '#0000FF'
  }
}
let blue = Color.BLUE // '#0000FF'
let getBlue = Color.getBlue() // '#0000FF'
  • generic classes
class Mac<Type> {
  version: Type;
  constructor(v: Type) {
    this.version = v;
  }
}

const b: Mac<string> = new Mac("hello!"); // 
  • Class Decorators

  • Property Decorators

  • Method Decorators

  • Accessor Decorators

  • Parameter Decorators

About

Short and simple bullet points about Typescript 💛

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published