您可以使用Union 组合器。
const decoder = pipe(
D.struct({
a: D.string
}),
D.intersect(
D.union(
D.struct({ b: D.string }),
D.struct({ c: D.string })
)
)
);
请记住,您将无法访问b
或c
不首先检查这些属性是否存在,因为 Typescript 无法知道您的对象中存在这两者中的哪一个。
type Decoder = D.TypeOf<typeof decoder>
declare const myDecoder: Decoder;
myDecoder.a // inferred as `string`
myDecoder.b // TYPE ERROR: Property 'b' does not exist on type '{ a: string; } & { c: string; }'
myDecoder.c // TYPE ERROR: Property 'c' does not exist on type '{ a: string; } & { b: string; }'
if ("b" in myDecoder) {
myDecoder.b // inferred as `string`
}
if ("c" in myDecoder) {
myDecoder.c // inferred as `string`
}
请注意,在检查这两个互斥属性时,您会收到类型错误。TypeScript 正确地推断出这是一种永远不会发生的情况(myDecoder
推断为never
在if
块内)
if ("b" in myDecoder && "c" in myDecoder) {
myDecoder.b // TYPE ERROR: Property 'b' does not exist on type 'never'.
myDecoder.c // TYPE ERROR: Property 'c' does not exist on type 'never'.
}