Typescript cheat sheet
Typescript cheat sheet
Control where diferent type operators naTow inside the new scope
"Lagreus
typeof datal =t
name String
interface Expect {
Cmatcher: boolean): string
SDoc comment attached to show in editors
Key points /** In bytes *
Cmatcher: string): boolean;
Used to describe
the shape of payloadSize: number
This property might not be on the otect
and can be extended by
objects,
others.
outOfStock?: boolean; These are two ways to describe a
property wnich is a function Get & Set
Aimost everyhing in Java>cnpt is update: (retryTimes: number) => void;
Obyects can have custom getters or setters
match their runtime behavior. update(retryTimes: number): void;
interface Ruler {
O: JsoNResponse
JSONResponse in
n call ths object via ()- (functions
JS are objects which can be called)
get sizeCO: number
set size(value: number| string);
Built-in Iype Primitives
new(s: string): JONResponse; YOu can use new on tre object
this ntertace descnbes
boolean string, number,
undefined, nutt any Tkey: string]: number;. Usage
Any property not described already is assumed
unknown never, vo1d to exist dnd di pGpE LES oi D E a
b1gint, symbol const r: Ruler
readonly body: string r.s1ze 12
Common Built-in JS Objects r.size "36"
els lypescnpt that a property
can not be
Date, Error, Array, Map, cnaye
Set, Regexp, Promise
api.data. status
ypeScript
Cheat Sheet Class Key points dlasses, and one hnastwoa few
ecdass or
type-specific extensions
runtime addibons.
to ES2015 JavaScript These features are Iypescnpt specitic language extensions which may
never make t to Javaacript with the curent syntax.
Dog Wolf
Object Literal Type
type Location {
Intersection Types Type from Func Return
Template Union Types
way to merge/extend types Re-use the retum value from a
X number function as a
type
y numDer, type Location: A template string can be used to combine and
const createFixtures = () . . manipulate text inside the type system.
x : number }& t y: number}
type Fixtures "en"I "pt" | "zh";
I / t x : number, y: number}
ReturnType<typeof createFixtures>
type SupportedLangs
type FooterLocaleIDs = "header" "footer"
Tuple Type
Type Indexing funct ion test(fixture: Fixtures) {} type ALlLocaleIDs =
Atuple is aspecial-cased array with known
Ypes at specme indexes. A way to extract and name from S(SupportedLangs}-${FooterLocaleIDs)_id'
type Data l a subset of à ype Type from Module I"en_header id""enfooter_id"
Location: Location type Response data: { . . } const data:
import(". /data"). data
"pt header_id" "pt_footer_id"
timestamp: strin9 "zh_header_ id" "zh_footer_id"
type Data = Response["data"]
..