I have a discriminated union:
type State =
{ tag: "A", /* other props */ }
| { tag: "B", /* other props */ }
Now I have a piece of code that can only work in a certain state:
// Somewhere
const state: State = …;
// And later elsewhere
if (state.tag !== "A") {
throw new AssertionError(…);
}
// Here I am sure `state` is of type A
Can I rewrite the condition into an assert function?
// Somewhere
const assertState = (state: State, tag: State["tag"]) => ???
// And later:
assertState(state, "A");
// Here I am sure `state` is of type A
CodePudding user response:
This can be done using assertion functions.
type State =
{ tag: "A", /* other props */ }
| { tag: "B", /* other props */ }
function assertState (condition: boolean): asserts condition {
if (!condition) {
throw new Error("message")
}
}
function test(){
let state: State = {} as any
assertState(state.tag === 'A')
state.tag // is only of type `A` now
}