我在使用时遇到一些问题io-ts
。我发现它确实缺乏文档,我取得的大部分进展都是通过 GitHub issues 取得的。不,我不明白 HKT,所以没有帮助。
基本上我在其他地方创建一个类型,type
它具有一组特定的键:
import * as S from 'io-ts/lib/Schema'
const someType = S.make(S => S.type({
id: S.string,
foo: S.string,
bar: S.boolean
}))
type T = S.TypeOf<typeof someType>
Run Code Online (Sandbox Code Playgroud)
我需要将其转换为仍需要一些键的部分。假设id
仍然需要密钥,那么在 TS 中定义类型将如下所示:
type PartlyPartial <T> = Partial<T> & { id: string }
Run Code Online (Sandbox Code Playgroud)
简单的!
现在,我希望能够在运行时执行此操作io-ts
,以便我可以使用该Guard
模块创建验证器函数。这是我到目前为止所得到的:
import * as G from 'io-ts/lib/Guard'
const propsBase = S.make(S => S.type({
id: S.string
}))
const partial = S.make(S => S.intersection(
someType(S) // …
Run Code Online (Sandbox Code Playgroud)