可以表示固定长度的阵列/";元组";使用@mojotech/json类型验证



使用@mojotech/json类型验证,类型[number, number, number]的解码器会是什么样子?

例如,

import { array, Decoder, number } from '@mojotech/json-type-validation'
type customTuple = [number, number, number]
const tupleDecoder: Decoder<customTuple> = array(number())

结果在:

Error:(5, 7) TS2322: Type 'Decoder<number[]>' is not assignable to type 'Decoder<[number, number, number]>'.
Type 'number[]' is not assignable to type '[number, number, number]'.
Property '0' is missing in type 'number[]'.

通过快速浏览文档,似乎没有真正的方法可以用customTuple实现这一点。您必须声明一个具有普通number[]类型的Decoder

最新更新