Reputation: 6406
I've created a scale down version of my problem.
The class must initialize the default value of its "status" property. It's of type T which extends the string litteral type "PossibleStatus" made of 3 possible strings.
Typescript doesn't accept this. Can you help me figure out why?
export type PossibleStatuses = 'idle' | 'on' | 'off';
export class StatefulNode<T extends PossibleStatuses> {
private status: T = 'idle';
constructor() { }
}
Upvotes: 0
Views: 38
Reputation: 276199
The following code demonstrates why you cannot initiate the status (type X = 'on'
extends PossibleStatuses
but doesn't include 'idle'
):
export type PossibleStatuses = 'idle' | 'on' | 'off';
export class StatefulNode<T extends PossibleStatuses> {
// Error
private status: T = 'idle';
}
// Because
const unsafe = new StatefulNode<'on'>();
Upvotes: 1