====== Cheat Sheet ====== === Usage === ''npm install typescript'' ''tsc'' === Primitive types === ^Primitive types^^ |Any type (explicitly untyped) |''any'' | |void type (undefined or null, use for function returns only) |''void'' | |Undefined type |''undefined'' | |Null type |''null''| |never type |''never''| |unknown type |''unknown''| |String (including ES6 multi-line string templates) |''string''| |Number |''number''| |Boolean |''boolean''| |object (may be an Object or non-primitive) |''object''| === Named types (interface, class, enum) === ^Named types (interface, class, enum)^^ |Interface | interface Child extends Parent, SomeClass { property: Type; optionalProp?: Type; optionalMethod?(arg1: Type): ReturnType; }| |Class | class Child extends Parent implements Child, OtherChild { property: Type; defaultProperty: Type = 'default value'; private _privateProperty: Type; private readonly _privateReadonlyProperty: Type; static staticProperty: Type; constructor(arg1: Type) { super(arg1); } private _privateMethod(): Type {} methodProperty: (arg1: Type) => ReturnType; overloadedMethod(arg1: Type): ReturnType; overloadedMethod(arg1: OtherType): ReturnType; overloadedMethod(arg1: CommonT): CommonReturnT {} static staticMethod(): ReturnType {} subclassedMethod(arg1: Type): ReturnType { super.subclassedMethod(arg1); } } | |Enum | enum Options { FIRST, EXPLICIT = 1, BOOLEAN = Options.FIRST | Options.EXPLICIT } enum Colors { Red = "#FF0000", Green = "#00FF00", Blue = "#0000FF" } | === Object type literals === ^Object type literals^^ |Object with implicit Any properties |''{ foo; bar; }''| |Object with optional property |''{ required: Type; optional?: Type; }''| |Hash map |''{ [key: string]: Type; }''| === Union and intersection types === ^Union and intersection types^^ |Union type |''let myUnionVariables: number | string;'' | |Intersection type |''let myIntersectionType: Foo & Bar;'' | === Arrays and tuples === ^Arrays and tuples^^ |Array of strings |''string[]'' or ''Array'' | |Array of functions that return strings |''{(): string; }[]'' or ''Array<() => string>'' | |Tuples |''let myTuple: [ string, number, boolean? ]; myTuple = [ 'test', 42 ];'' | === Functions === ^Functions^^ ^Function |''{ (arg1: Type, argN: Type): Type; }'' or ''(arg1: Type, argN: Type) => Type;'' | ^Constructor |''{ new (): ConstructedType; }'' or ''new () => ConstructedType;'' | ^Function type with optional param |''(arg1: Type, optional?: Type) => ReturnType'' | ^Function type with rest param |''(arg1: Type, ...allOtherArgs: Type[]) => ReturnType'' | ^Function type with static property |''{ (): Type; staticProp: Type; }'' | ^Default argument |''function fn(arg1: Type = 'default'): ReturnType {}'' | ^Arrow function |''(arg1: Type): ReturnType => {}'' or ''(arg1: Type): ReturnType => Expression'' | ^this typing |''function fn(this: Foo)'' | === Generics === ^Generics^^ ^Function using type parameters |''(items: T[], callback: (item: T) => T): T[]'' | ^Interface with multiple types |''interface Pair { first: T1; second: T2; }'' | ^Constrained type parameter |''(): T'' | ^Default type parameter |''(): T'' | ^Constrained and default type parameter |''(): T'' | === Partial and mapped types === ^Partial and mapped types^^ ^Partial type |''Partial<{ x: number; y: number; z: number; }>'' is equivalent to ''{ x?: number; y?: number; z?: number; }'' | ^Readonly type |''Readonly<{ x: number; y: number; z: number; }>'' is equivalent to ''{ readonly x: number; readonly y: number; readonly z: number; }'' | ^Pick type |''Pick<{ x: number; y: number; z: number; }, 'x' | 'y'>'' is equivalent to ''{ x: number; y: number; }'' | ^Record type |''Record<'x' | 'y' | 'z', number>'' is equivalent to ''{ x: number; y: number; z: number; }'' | === Conditional types === ^Conditional types^^ ^Conditional types |''declare function createLabel(idOrName: T): T extends number ? Id : Name;'' | ^Exclude |''type Excluded = Exclude;'' is equivelant to ''number'' | ^Extract |''type Extracted = Extract;'' is equivelant to ''string'' | ^NonNullable |''type NonNull = NonNullable;'' is equivalent to ''string | number'' | ^ReturnType |''type ReturnValue = ReturnType<() => string>;'' is equivalent to ''string'' | ^InstanceType |''class Renderer() {} type Instance = InstanceType;'' is equivalent to ''Renderer'' | === Other === ^Other^^ ^Type of a variable |''typeof varName'' |