Suppose there is a union type Thing
grouping together types Foo
, Bar
and Baz
with the discriminating property tag
.
interface Foo {
tag: 'Foo'
foo: string
}
interface Bar {
tag: 'Bar'
bar: number
}
interface Baz {
tag: 'Baz'
baz: boolean
}
type Union = Foo | Bar | Baz
Now I would like to create a mapped type where I would iterate over tags of Union
and use the corresponding interface in the type mapped to the tag. The question is: Is it possible to retrieve a type from a union type by its tag value?
interface Tagged {
tag: string
}
type TypeToFunc<U extends Tagged> = {
// Is it possilbe to retrieve the type for the given tag from the union type?
// What to put in place of the ???
readonly [T in U['tag']]: (x: ???) => string
}
const typeToFunc: TypeToFunc<Union> = {
// x must be of type Foo
Foo: x => `FOO: ${x.foo}`,
// x must be of type Bar
Bar: x => `BAR: ${x.bar}`,
// x must be of type Baz
Baz: x => `BAZ: ${x.baz}`,
}
If not, is there any other way to achieve this kind of mapping?