Ali Rahmani
Ali Rahmani

Reputation: 11

Narrow down typescript type

I have a type like this:

type MyType = "One" | "Two" | "Three" | "Four" | "Five"

Now, in some cases, not all of the values of MyType are valid. So I want to have a type, which can have the same values as MyType, except a few of them. like this:

type OtherType = "One" | "Two" | "Five"

Basically, I want the second type to be a limited version of the first type. I'm not sure if this is possible; That's why I'm asking for help to make sure :)

I know I probably should make two different types BTW, but if this would be possible, it would save me from a lot of pain!

Thanks!

Upvotes: 1

Views: 29

Answers (1)

jtbandes
jtbandes

Reputation: 118781

You can use Exclude for this:

type OtherType = Exclude<MyType, "Three" | "Four">;

Upvotes: 2

Related Questions