Skip to content

Instantly share code, notes, and snippets.

@antfu
Last active July 23, 2020 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antfu/ea8325bebcafc7361185981b321003fe to your computer and use it in GitHub Desktop.
Save antfu/ea8325bebcafc7361185981b321003fe to your computer and use it in GitHub Desktop.
type Unexact<T> =
T extends null
? null
: T extends undefined
? undefined
: T extends number
? number
: T extends boolean
? boolean
: T extends string
? string
: T extends Promise<infer R>
? Promise<Unexact<R>>
: T extends (...args: infer A) => infer R
? (...args: Unexact<A>) => Unexact<R>
: T extends object
? { -readonly [K in keyof T]: Unexact<T[K]> }
: never
// usage
type a = Unexact<'1234'> // string
type b = Unexact<() => true> // () => boolean
type c = Unexact<{ a: 1234, b: 'foo' }> // { a: number, b: string }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment