I am attempting to devise an interface in typescript that resembles the following:
type MoveSpeed = "min" | "road" | "full";
interface Interval {
min?: number,
max?: number
}
interface CreepPlan {
[partName: string] : Interval;
move?: MoveSpeed;
}
Unfortunately, this syntax is considered invalid by the compiler. It generates the error message
Property 'move' of type '"min" | "road" | "full" | undefined' is not assignable to string index type 'Interval'.
.
The compiler option strictNullChecks:true
is being utilized, leading to the inclusion of undefined
implicitly for the move?
key.
However, a valid alternative syntax would be:
type MoveSpeed = "min" | "road" | "full";
interface Interval {
min?: number,
max?: number
}
interface CreepPlan {
[partName: string] : Interval;
move: MoveSpeed; // 'move' becomes mandatory
}
My intention is to convey the concept that "CreepPlan is comprised of string:Interval pairs, with the exception of the optional 'move' key which represents a string:MoveSpeed pair." Is there a way to express this idea effectively in TypeScript?