目前我正在开发一个具有 fp-ts 和 io-ts 堆栈的项目。我正在尝试验证我们从后端获得的所有响应io-ts
。我开始知道io-ts
没有简单的方法来制作optional
. typescript
并从这个问题中找到了一种解决方法来制作带有可选字段的对象。
我想创建看起来像这样的类型。
type finalType = {
req: string;
opt?: string;
};
在我们的项目中,我们有这个runDecoder
函数来验证响应数据的io-ts
类型。它适用于非可选的普通io-ts
类型。
但是当它试图验证在类型的帮助下成为可选的类型时,问题就出现了t.intersection
。这是带有示例的代码
import * as Either from "fp-ts/lib/Either";
import * as io from "io-ts";
export const runDecoder = <T extends io.Props>(type: io.TypeC<T>) => (
data: unknown
) => {
const result = type.decode(data);
if (Either.isLeft(result)) {
return Error("error");
}
return result.right;
};
// I want to create type something like this
// type finalType = {
// req: string;
// opt?: string;
// };
const OptionType = io.partial({
opt: io.string
});
const RequiredType = io.type({
req: io.string
});
const FinalType = io.intersection([RequiredType, OptionType]);
type resultType = io.TypeOf<typeof FinalType>;
const respose:resultType = {
req: "str"
};
const decoded = runDecoder(FinalType)(respose);
我得到的错误是
Argument of type 'IntersectionC<[TypeC<{ req: StringC; }>, PartialC<{ opt: StringC; }>]>' is not assignable to parameter of type 'TypeC<Props>'.
Property 'props' is missing in type 'IntersectionC<[TypeC<{ req: StringC; }>, PartialC<{ opt: StringC; }>]>' but required in type 'TypeC<Props>'.
我试图理解错误,但我无法用runDecoder
方法弄清楚这里有什么问题。这是Codesandbox链接。任何帮助将不胜感激。