I have 2 identical enums
enum State {
On,
Off,
}
enum Status {
On,
Off,
}
I would like to be able to affect a state value to the status one
const stateOn: State = State.On;
const statusOn1: Status = stateOn; // But we cannot because State !== Status
So trying to cast it will give the following:
const statusOn2: Status = stateOn as Status; // Error, cannot cast a State to a Status
const statusOn3: Status = stateOn as unknown as Status; // Okay but risky
Therefore would it be possible to create a conditional type which would ensure both enums are the same ?
Something like :
type castEnum<T, U> = Extract<keyof typeof T, keyof typeof U> extends keyof typeof U ? U : never;
To then be able to affect the variable like (idea):
const statusOn4: Status = stateOn as castEnum<State, Status>;