Answer the question
In order to leave comments, you need to log in
How to get property types of a combined type in TypeScript?
Hello!
It is better to show the problem with an example, so here ... I have such a combined type
type Creature = {
type: "people";
subtype: "adult";
params: IPeopleAdultParams;
} | {
type: "animal";
subtype: "cat";
params: IAnimalCatParams;
}
creatures.push({
type: 'animal',
subtype: 'cat',
params: {
...
}});
function push(type: ?, subtype: ??, params: ???) {
creatures.push({type, subtype, params})
}
Answer the question
In order to leave comments, you need to log in
type Creature = {
type: "people";
subtype: "adult";
params: IPeopleAdultParams;
} | {
type: "animal";
subtype: "cat";
params: IAnimalCatParams;
}
type allTypes = Creature['type']
When I answered you here in the example there was a CreatureBasis type from which the Creature type was already built through calculations on types.
You can also use it to build your function:
https://www.typescriptlang.org/play?#code/C4TwDgpg...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question