Is it possible to create a mapped type, where the mapping would depend on the type of a property. For instance, assume that I want to map all String
properties to a type Foo
and all other to Bar
. So, I'd like to do something like:
type Mapped<T> = {
[P in keyof T]: T[P] === String ? Foo : Bar
}
Is there any syntax to achieve this?
You will have to install typescript@next
or wait for version 2.8 to be officially released.
You can then achieve what you want like this:
type Mapped<T> = {
[P in keyof T]: T[P] extends string ? Foo : Bar
}
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.