简体   繁体   中英

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!

You can use Exclude for this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM