简体   繁体   中英

How can I write an assert function to assert a discriminated union type?

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

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
}

Playground

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM