I am working on an interface that has an action property. Depending on the action type, I want to add an optional property to the interface. The current interface looks like this:
const FIGHT = `fight`;
const SWIM = `swim`;
const DANCE = `dance`;
type ActionType =
| typeof FIGHT
| typeof SWIM
| typeof DANCE
interface Creator {
actionType: ActionType;
typeId: string;
}
If the actionType is set to SWIM, then we need to include an additional property in the Creator interface, like so:
interface Creator {
actionType: ActionType;
typeId: string;
trained?: string;
}
In my solution, I have created a Creator
.
interface CreatorBase {
actionType: ActionType;
typeId: string;
}
interface CreatorSwim extends CreatorBase {
actionType: typeof SWIM;
trained?: string;
}
export type Creator =
| CreatorBase
| CreatoeSwim
However, when I try to access the swim
property of an object of type Creator, I receive the error TS2339: Property 'trained' does not exist on type 'Creator'.