0.1.9 • Published 3 years ago

@x_m/type-utils v0.1.9

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

@x_m/type-utils

typescript 类型辅助函数

install

yarn add @x_m/type-utils

examples

PickOneOf
StructAs


PickOneOf

from: https://dev.to/maxime1992/implement-a-generic-oneof-type-with-typescript-22em

interface Group {
  num: number[]
  str: string[]
  boo: boolean[]
}

/**
 * PickOneOfGroup is {
 *   num: number[]
 *   str: undefined
 *   boo: undefined
 * } | {
 *   num: undefined
 *   str: string[]
 *   boo: undefined
 * } | {
 *   num: undefined
 *   str: undefined
 *   boo: boolean[]
 * }
 */
type PickOneOfGroup = PickOneOf<Group>

↑ examples ↑

StructAs

interface Group {
  num: number[]
  str: string[]
  boo: boolean[]
}

/**
 * StructedGroup is {
 *   type: 'num'
 *   value: number[]
 * } | {
 *   type: 'str'
 *   value: string[]
 * } | {
 *   type: 'boo'
 *   value: boolean[]
 * }
  */
type StructedGroup = StructAs<Group, 'type', 'value'>

↑ examples ↑