Typescript Overview

Types

string, number, any, never, array[], boolean, tuple, enum, void, null, undefined, Object

can assert that a type is correct thing as Type

Variables

Interfaces

Classes

Functions

Arrow functions

Normally, this is set when the function is called. Arrow functions capture the this where the function is created rather than where it is invoked.

Generics

Enums

Type Compatibility

x is compatible with y if y has at least the same members as x

Advanced Types

Intersection types

Person & Serializable & Loggable has all the members of all three types

Union types

number | string one type or the other Can only access members that are common to the union

Type guards

Restrict something to a certain subtype in a particular scope Custom type guards typeof instanceof like typeof but looks at the constructor function

Nullable types

Can use flag —strictNullChecks to prevent any variable from being assigned null, unless you use a union type containing null.

Type aliases

These create a new name for an existing type.

String literal types and numeric literal types

Specify the exact value a string must have. Other strings will not be able to be assigned. Useful when combined with union types. Same things exists with numbers

Conditional types

T extends U ? X : Y Choose the type based on some condition Lots more going on here.

Iterators and Generators

for (let x of list) // loops over the values of list
for (let i in list) // loops over the keys of list

"of" can only be used on Symbol.iterator things, while "in" can be used on any object to enumerate it’s properties

Modules

Namespaces

Namespaces and Modules

Don’t mix the two too much. Node uses modules almost exclusively

Declaration Merging

If you define something, ie an interface, multiple times, ts will combine the members from both. You can merge a class and a namespace to get the effect of an inner class.

JSX

An xml like language that compiles to js, used largely in react. Need .tsx extension

Decorators

Triple-Slash Directives

Compiler directives of form /// <reference path="..." /> Only valid at top of file

Utility Types