2

使用最新的https://github.com/gcanti/io-ts/,我想建模 to 的属性result包含NodeLsStatusResponse类型NodeStatusNodeStatus404in (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
})
4

1 回答 1

3

我能够通过对只读数组使用联合来解决这个问题:

export const NodeLsStatusResponse = t.type({
  code: t.literal('OK'),
  result: t.readonlyArray(t.union([NodeStatus, NodeStatus404])),
})
于 2020-05-01T21:40:33.640 回答