Appendix B. TypeScript cheat sheet

This cheat sheet is not exhaustive. It covers the TypeScript syntax subset used in this book. For a full TypeScript reference, see http://www.typescriptlang.org/docs.

Table B.1. Primitive types

Type

Description

boolean Can be true or false.
number 64-bit floating-point number.
string UTF-16 Unicode string.
void Type used as return type for functions that don’t return meaningful values.
undefined Can be only undefined. Represents, for example, a variable that was declared but not initialized.
null Can be only null.
object Represents an object or nonprimitive type.
unknown Can represent any value. Type-safe, so it isn’t implicitly converted to another type.
Any Bypasses type checking. Type-unsafe and automatically converted to any other type.
Never Cannot represent any value

Table B.2. Nonprimitive types (continued)

Example

Description

string[] Array types are denoted by [] after the type name—in this case, an array of strings.
[number, string] Tuples are declared as a list of types within []—in this case, a number and a string, such as [0, "hello"].
(x: number, y: number) => number; Function types are declared as a list of arguments in (), then =>, then the return type.
enum Direction {
North,
East,
South,
West,
}
Enumerations are declared with the keyword enum. In this case, a value can be one of the literals North, East, South, or West.
type Point {
X: number,
Y: number
}
A type with X and Y properties of type number.
interface IExpression {
evaluate(): number;
}
An interface with an evaluate() method that returns a number.
class Circle extends Shape
implements IGeometry {
// ...
}
Circle class extending the Shape base class and implementing the IGeometry interface.
type Shape = Circle | Square; Union types are declared as an |-separated list of types. Shape is either a Circle or a Square.
type SerializableExpression
= Serializable & Expression;
Intersection types are declared as a &-separated list of types. SerializableExpression has both all members of Serializable and all members of Expression.
Table B.3. Declarations

Declaration

Description

let x: number = 0; Declares a variable named x of type number with the initial value 0.
let x: number; Declares a variable named x of type number. Must be assigned a value before use.
const x: number = 0; Declares a constant name x of type number with the value 0. x can’t be changed.
function add(x: number, y: number)
: number {
return x + y;
}
Declares a function add() that takes two arguments, x and y of type number, and returns a number.
(x: number, y: number) => x + y; A lambda (anonymous function) that takes two arguments and returns their sum.
namespace Ns {
export function func(): void {
}
}

Ns.func();
Namespaces are declared with the namespace keyword. Declarations inside a namespace must be prefixed with export to be visible outside the namespace.
class Example {
a: number = 0;
private b: number = 0;
protected c: number = 0;
readonly d: number;

constructor(d: number) {
this.d = d;
}

getD(): number {
return this.d;
}
}

let instance: Example
= new Example(5);
All class members are public by default. Can also be protected (visible to derived classes) and private (visible only inside the class). Properties can also be readonly, in which case they can’t be modified after they are assigned. Unless properties allow undefined as a value, they must be initialized either inline or with the constructor. The constructor of any class is constructor(). References to class members within the class must always be prefixed with this. Objects are instantiated with new, which invokes the constructor.
declare const Sym: unique symbol; A Symbol guaranteed to be unique. No two constants declared as unique symbol can ever be equal.
Table B.4. Generics

Example

Description

function identity<T>(value: T): T {
return value;
}

let str: string =
identity<string>("Hello");
A generic function has one or more type parameters between <>, before the argument list. identity() has one type argument T. It takes a value of type T and returns it. Specifying a concrete type between <> instantiates the generic function. identity<string>() is the identity() function where T is string.
class Box<T> {
value: T;

constructor(value: T) {
this.value = value;
}
}

let x: Box<number> = new Box(0);
A generic class has one or more type parameters between <>, after the class name. Box has a property value of type T. Specifying a concrete type between <> instantiates the generic class. Box<number> is the Box class where T is number.
class Expr<T extends IExpression> {
/* ... */
}
A generic constraint is declared after the generic type parameter. In this example, T must support the IExpression interface.
Table B.5. Type casts and type guards

Example

Description

let x: unknown = 0;

let y: number = <number>x;

type Point = {
x: number;
y: number;
}

function isPoint(p: unknown):
p is Point {
return
((<Point>p).x
!== undefined) &&
((<Point>p).y
!== undefined);
}

let p: unknown = { x: 10, y: 10 };

if (isPoint(p)) {
// p is of type Point here
p.x -= 10;
}
Specifying a type between <> before a value reinterprets the value as that type. x can be assigned to y only after being explicitly reinterpreted as number. A type predicate is a boolean that states that a variable is of a certain type. If we reinterpret p as a Point, and it has both an x and a y member (neither is undefined), p is a Point. Within an if statement where a type predicate is true, the tested value is automatically reinterpreted as having that type.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.96.159