Things You Need to Know About TypeScript

MING WU
2 min readAug 13, 2019

This passage is not finished and is still in progress…

  1. Numbers:
const bigNumber:number=1_100_200_3000 //much easier to read;

what this compiles to javascript is

const bigNumber=11002003000;

In typescript, using underscore makes your code easier to read.

2. Add “!” to tell ts to ignore it

3. keyof

interface IPet{
name:string,
age:number,
favouritePark?:string
}
type ReadonlyPet{
+readonly [K in keyof IPet]-?:IPet[K]
}

“+” clearly indicates that we are adding a “readonly” modifier and the “-” clearly indicates that we are deleting “optional” modifier.

4. types and interface

They are similar to some extent

type Eat=(food:string)=>void;interface Eat{
(food:string):void
}

Another example:

type Cat = IPet & IFeiline;
interface ICat extends IPet,IFeline{
}
//definition
interface IPet{
pose():void;
}
interface IFeline{
nightvision:boolean
}

Differences:

type PetType=IDog|ICat;interface IPet extends PetType{
//this is not allowed
}

interface can merge but types cannot merge

interface foo{
a:string
}
interface foo{
b:string
}
let foo:Foo //type does not support

5. unknown or any

unknown is very strict and does not allow you to do any unless you specify its types.
any is the least strict type. You can do anything.

6. conditional types

interface Book{
id:string
}
interface TV{
id:number,
diagonal:number
}
interface IItemService{
getItem<T extends string | number>(id:T):T extends string?Book:TV
}
let itemService:IItemService;
const book=itemService.getItem('10');
const tv=itemService.getItem(10);

--

--

MING WU

aws solution architect associate, developer associate, top 11% in stackoverflow, https://linktr.ee/mingw