如何在 io-ts 中定义混合数组?
How to define mixed array in io-ts?
使用最新的 https://github.com/gcanti/io-ts/,我想将 NodeLsStatusResponse
的 属性 result
建模为包含 NodeStatus
或 NodeStatus404
类型的对象在 (t.readonlyArray)
如何定义这种关系io-ts?
export const Connection = t.union([t.literal('a'), t.literal('b')])
export type Connection = t.TypeOf<typeof Connection>
export const NodeStatus = t.type({
connection: Connection,
nodeId: t.string,
})
export const NodeStatus404 = t.type({
connection: Connection,
host: t.string,
})
export type NodeStatus = t.TypeOf<typeof NodeStatus>
export type NodeStatus404 = t.TypeOf<typeof NodeStatus404>
export const NodeLsStatusResponse = t.type({
code: t.literal('OK'),
result: t.readonlyArray(NodeStatus), /// <<< I would like to have a mixed array here
})
我能够通过对只读数组使用联合来解决这个问题:
export const NodeLsStatusResponse = t.type({
code: t.literal('OK'),
result: t.readonlyArray(t.union([NodeStatus, NodeStatus404])),
})
使用最新的 https://github.com/gcanti/io-ts/,我想将 NodeLsStatusResponse
的 属性 result
建模为包含 NodeStatus
或 NodeStatus404
类型的对象在 (t.readonlyArray)
如何定义这种关系io-ts?
export const Connection = t.union([t.literal('a'), t.literal('b')])
export type Connection = t.TypeOf<typeof Connection>
export const NodeStatus = t.type({
connection: Connection,
nodeId: t.string,
})
export const NodeStatus404 = t.type({
connection: Connection,
host: t.string,
})
export type NodeStatus = t.TypeOf<typeof NodeStatus>
export type NodeStatus404 = t.TypeOf<typeof NodeStatus404>
export const NodeLsStatusResponse = t.type({
code: t.literal('OK'),
result: t.readonlyArray(NodeStatus), /// <<< I would like to have a mixed array here
})
我能够通过对只读数组使用联合来解决这个问题:
export const NodeLsStatusResponse = t.type({
code: t.literal('OK'),
result: t.readonlyArray(t.union([NodeStatus, NodeStatus404])),
})