Installing TypeScript Now you can use the TypeScript compiler running tsc. To test whether a TypeScript file compiles create a simple file: and then run tsc index.ts. You thought it was as simple as that but you end up with an error: The reason is that name is already defined on the window object (global […]

Basic and built-in Types Boolean, Number, String, Array, Enum, Void, Null, Undefined, Never, Any. Union Types A Union Type is created by joining multiple types via the pipe symbol |, for example A | B. A Union of Type A | B means… the object (having this union type) must either be A or B […]

Module Resolution You can import modules relatively or Non-relatively: How does TypeScript find modules that you are referencing? Either via Classic or Node –moduleResolution strategy. Classic is the default when emitting AMD, System or ES2015 modules. Node is the default when emitting CommonJS or UMD modules. Resolving Classic Relative Imports: Resolving Classic Non-relative Imports: Resolving […]

Type Guards are used to check whether you are dealing with a certain type or not. Getting from a more general type to a more specific type is called Narrowing. There are different ways to ‘type guard’, depending on what to test against. You can test against native values (string, number, boolean etc.), instances of […]

Utility types let you create types based on other types. Have a look at the official TypeScript page for example about utility types. Partial<Type> Constructs a type with all properties of Type set to optional. Required<Type> Constructs a type consisting of all properties of Type set to required. Opposite of Partial. Readonly<Type> Constructs a type […]

In the following example we have doLog, that checks if a log properties exists on the passed in object. If yes, then it assumes it is a function and executes it. That’s a bad, because we cannot guarantee that log is actually a logging function. The following example would break doLog: Another reasons why this […]

A short article that displays three ways on how you can handle errors using async and await.

This is how you can force TypeScript to always have at least one item in an array. Or in other words: to not allow empty arrays. You can also force it to have at least 2, 3 etc. items: This came in handy lately when I was implementing a ColorSelection component in React: Having it […]