Skip to content
a list of typescript helper libraries
Branch: master
Clone or download
Latest commit 269e0d5 Sep 3, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE Initial commit May 2, 2019
README.md add rex-tils Sep 3, 2019

README.md

typescript-utilities-guide

a list of typescript helper libraries. advanced guides in typescript-cheatsheets will assume knowledge of these and refer people here.

Utility Types

Be familiar with the Utility Types that ship with TS. On top of that, here are handy Utility Types often used by TS practitioners, with explanation on what they do and how they can help. We will assume knowledge of mapped types and conditional types like Exclude<T, U> and ReturnType<T> but try to build progressively upon them.

Note: If you are new to conditional types, I highly recommend DJSheldrick's blogpost and talk on Conditional Types in TypeScript

Omit<T, K extends keyof T>: Subtract keys from one interface from the other.
/**
 * Subtract keys from one interface from the other.
 *
 * @example
 * interface One { one: string }
 * interface Three { one: string, two: string }
 *
 * type Two = Omit<Three, keyof One>;
 *
 * // The type of Two will be
 * interface Two { two: string }
 */
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

You can also supply string literals to omit:

type SettingsPageProps = Omit<
  ServerConfig,
  "immutableSetting1" | "invisibleSetting2"
>;
Optionalize<T extends K, K>: Remove from T the keys that are in common with K
/**
 * Remove from T the keys that are in common with K
 */
type Optionalize<T extends K, K> = Omit<T, keyof K>;

An example usage is in our HOC section below.

Nullable<T> or Maybe<T>: Make a Type into a Maybe Type
/**
 * Make a Type into a Maybe Type
 */
type Nullable<T> = T | null
type Maybe<T> = T | undefined

Your choice of null or undefined depends on your approach toward missing values. Some folks feel strongly one way or the other.

Dictionary<T>: Dictionary of string, value pairs
/**
 * Dictionary of string, value pairs
 */
type Dictionary<T> = { [key: string]: T }

[key: string] is a very handy trick in general. You can also modify dictionary fields with Readonly or make them optional or Omit them, etc.

There also exist helper type libraries:

seriously, check some of these out, they represent a ton of accumulated typescript experience.

Misc

Codegen from TypeScript

Data Structures

https://github.com/basarat/typescript-collections

You can’t perform that action at this time.