Skip to content

Instantly share code, notes, and snippets.

@noveogroup-amorgunov
Last active July 24, 2023 05:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save noveogroup-amorgunov/ad42851e96289949433dfc1b54f147e7 to your computer and use it in GitHub Desktop.
Save noveogroup-amorgunov/ad42851e96289949433dfc1b54f147e7 to your computer and use it in GitHub Desktop.
Boilerplate type definition file for YOUR projects
declare global {
/**
* (1) Global TypeScript-helpers
*
* See built-in utility types to TypeScript
* @see https://www.typescriptlang.org/docs/handbook/utility-types.html
*/
/**
* Get union type with generic and null
* @example
* // Expect: "User | null"
* type MaybeUser = Nullable<User>;
*/
export type Nullable<T> = T | null
/**
* Get union type of object keys; alias for keyof T
* @example
* // Expect: "foo | baz"
* type ObjectKeys = Keys<{foo: 1, baz: 2}>;
*/
export type Keys<T extends Record<string, unknown>> = keyof T
/**
* Get union type of object values
* @example
* // Expect: "1 | 2"
* type ObjectValues = Values<{foo: 1, baz: 2}>;
*/
export type Values<T extends Record<string, unknown>> = T[Keys<T>]
/**
* Typed type alias for Record
* @example
* type NumericHash = Indexed<string, number>;
*/
export type Indexed<K = string, T = unknown> = { [key: K]: T }
/**
* Create brand type
* @example
* type ProductId = Brand<number, 'ProductId'>
*/
export type Brand<K, T> = K & { __brand: T }
/**
* (2) Global type alias
*/
export type Phone = string
export type Email = string
export type Uuid = string
export type DateIso = string
export type Timestamp = number
export type Penny = number
export type Url = string
export type ColorRgb = `rgb(${number}, ${number}, ${number})`
export type ColorHex = `#${string}`
export type Color = ColorRgb | ColorHex
/**
* (3) Global shared kernel
* @see https://bespoyasov.me/blog/clean-architecture-on-frontend/#into-detail-shared-kernel
*/
// TODO: Replace the types below with your shared kernel types
export type ApiError = {
statusCode: number
message: string
}
export type ApiResponse<T> = { data: T } | ApiError
}
export {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment