Skip to content

Instantly share code, notes, and snippets.

@SHND
Last active December 11, 2021 22:38
Show Gist options
  • Save SHND/d9c9ab59c13106d8307ae3d2f65ac1cd to your computer and use it in GitHub Desktop.
Save SHND/d9c9ab59c13106d8307ae3d2f65ac1cd to your computer and use it in GitHub Desktop.
Typescript Type calculation and manipulation (Utility Types)
/**
* TypeScript Type Calculations (Utility Types)
*/
/** ==================
* Interface
==================== */
/** Example */
interface Person {
name: String;
age?: number;
isMale ?: boolean;
}
/* ----- Partial ----- */
type PersonOptional = Partial<Person>
// type PersonOptional = {
// name?: String | undefined;
// age?: number | undefined;
// isMale?: boolean | undefined;
// }
/* ----- Required ----- */
type PersonRequired = Required<Person>
// type PersonRequired = {
// name: String;
// age: number;
// isMale: boolean;
// }
/* ----- Readonly ----- */
type PersonReadonly = Readonly<Person>
// type PersonReadonly = {
// readonly name: String;
// readonly age?: number | undefined;
// readonly isMale?: boolean | undefined;
// }
/* ----- Pick ----- */
type PersonPick = Pick<Person, 'name' | 'age'>
// type PersonPick = {
// name: String;
// age?: number | undefined;
// }
/* ----- Omit ----- */
type PersonOmit = Omit<Person, 'age' | 'isMale' | 'x'>
// type PersonOmit = {
// name: String;
// }
/** ==================
* Record
==================== */
type records = Record<string, Person>
// type records = {
// [x: string]: Person;
// }
/** ==================
* Union
==================== */
/** Example */
type MixType = number | 'none' | 'all' | null
/* ----- Exclude ----- */
type MixTypeExclude = Exclude<MixType, number | 'all'>
// type MixTypeExclude = 'none' | null
/* ----- Extract ----- */
type MixTypeExtract = Extract<MixType, number | 'all' | 'x'>
// type MixTypeExtract = number | "all"
/* ----- NonNullable ----- */
type MixTypeNonNullable = NonNullable<MixType> // removes undefined as well
// type MixTypeNonNullable = number | "none" | "all"
/** ==================
* Function
==================== */
/** Example */
type FuncType = (this: boolean, name: string, maxAge: number) => boolean | null
/* ----- Parameters ----- */
type FuncTypeParameter = Parameters<FuncType>
// type FuncTypeParameter = [name: string, maxAge: number]
/* ----- ReturnType ----- */
type FuncTypeReturnType = ReturnType<FuncType>
// type FuncTypeReturnType = boolean | null
/* ----- ThisParameterType ----- */
type FuncTypeThisParameterType = ThisParameterType<FuncType>
// type FuncTypeThisParameterType = boolean
/* ----- OmitThisParameter ----- */
type FuncTypeOmitThisParameter = OmitThisParameter<FuncType>
// type FuncTypeOmitThisParameter = (name: string, maxAge: number) => boolean | null
/** ==================
* String
==================== */
/** Example */
type StrType = 'hello' | 'Bye'
/* ----- Uppercase ----- */
type StrTypeUppercase = Uppercase<StrType>
// type StrTypeUppercase = "HELLO" | "BYE"
/* ----- Lowercase ----- */
type StrTypeLowercase = Lowercase<StrType>
// type StrTypeLowercase = "hello" | "bye"
/* ----- Capitalize ----- */
type StrTypeCapitalize = Capitalize<StrType>
// type StrTypeCapitalize = "Bye" | "Hello"
/* ----- Uncapitalize ----- */
type StrTypeUncapitalize = Uncapitalize<StrType>
// type StrTypeUncapitalize = "hello" | "bye"
/** ==================
* JavaScript Global
==================== */
/* ---- ConstructorParameters ----- */
type ErrorConstructorParameters = ConstructorParameters<ErrorConstructor>
// type ErrorConstructorParameters = [message?: string | undefined]
type RegExpConstructorParameters = ConstructorParameters<RegExpConstructor>
// type RegExpConstructorParameters = [pattern: string | RegExp, flags?: string | undefined]
// TODO: InstanceType
// TODO: ThisType
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment