type Group = "A" | "B" | "C"
I would like to declare an io-ts type to do runtime checks on a string union type.
I'm not sure if this is already possible with the current implementation of io-ts, so please enlighten me if that's the case
Group: t.string
Group: t.stringUnion(["A", "B", "C")
| Software |
| ---------- |
"io-ts": "^2.1.3",
"io-ts-promise": "^2.0.2",
"typescript": "3.9.7"
With the stable API, you can create a union of literals using keyof. From the readme, documentation.
With the experimental API, you can use union and literal:
const Group = D.union(D.literal('A'), D.literal('B'), D.literal('C'))
With the experimental API, you can use union and literal:
@DenisFrezzato FYI the experimental D.literal takes one or more literals
import * as D from 'io-ts/Decoder'
const Group = D.literal('A', 'B', 'C')
Most helpful comment
@DenisFrezzato FYI the experimental
D.literaltakes one or more literals