Martin Wedvich
Martin Wedvich

Reputation: 2266

Extract constraint from generic type

Given the following generic type:

interface SomeWrapper<TValue extends string | number | boolean | null = string> {
  // ...
}

Is there any way for me to extract the constraint into a type?

type SomeConstraint = DoSomeMagicHere<SomeWrapper> // = string | number | boolean | null

Upvotes: 1

Views: 176

Answers (1)

vitoke
vitoke

Reputation: 748

I'm not sure what the use of it is, but you can extract it like this:

interface SomeWrapper<
  TValue extends string | number | boolean | null = string
> {
  // ...
}

type SomeConstraint = any extends SomeWrapper<infer R> ? R : never;
// = string | number | boolean | null

Upvotes: 4

Related Questions