- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 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>
我需要将其转换为仍需要一些键的部分。假设仍然需要 id
键,则在 TS 中定义类型如下:
type PartlyPartial <T> = Partial<T> & { id: string }
简单!
现在,我希望能够通过 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) // needs to be partial,
propsBase(S)
))
// So that this returns `true`
partial(G.guard).is({
id: 'some id',
bar: true
})
但是 someType(S)
将类型定义为具有所需的所有键,但其中没有一个是必需的。
这让我发疯,任何帮助将不胜感激!
最佳答案
我不是 io-ts 方面的专家,但仔细观察一下,我认为这不是一件容易实现的事情。 Schemable
中的方法从其他架构中构建架构。 type()
方法需要 properties
对象,其键是所需对象的键,其属性对于每个属性都是可架构的。 partial()
方法采用相同的 properties
对象,但构建一个可架构,其中属性是可选的。您可以看到这些如何允许您构建架构;你有一个properties
包含您需要测试的所有键的对象。
但给定类型 T
的架构,一般不可能将其转换为 Partial<T>
。 properties
传入 type()
的对象在其结果中不可访问。它已经丢失了;扔掉。比如说,如果您拥有的只是一个返回 true
的类型保护函数。当且仅当给定 T
类型的值时,您不能使用该函数来创建另一个生成 true
的函数当且仅当给定 Partial<T>
类型的值时。您需要T
的更多详细信息来自守卫功能之外的某些来源,例如 properties
现在已经消失的对象。
因此,您无法直接组合架构来完成此任务。
执行此操作的唯一方法是编写您自己的 Schemable
存储了有关 Schema
的足够信息调用它以便它可以返回一个新模式,例如对 S.type()
的顶级调用替换为对 S.partial()
的调用。如果您想查看跨并集或向下到交叉点的部分分布,即使这样也不是完美的。本质上它必须对Schema
进行手术。 ,展开对 S.type()
的顶级调用找到迷失的地方properties
对象,并用 S.partial()
重新包装它。
很容易就能将所有调用转为 S.type()
进入S.partial()
(只需将旧架构替换为新架构,其 type
属性是 partial
属性的副本),但这最终会使任何嵌套对象以及顶级对象成为部分对象。
这是我所做的可怕的事情,但似乎有效:
import { URIS, Kind } from "fp-ts/lib/HKT";
function partial<T extends object>(obj: S.Schema<T>): S.Schema<Partial<T>> {
const partSchemable = <S extends URIS>(S: Schemable<S>) =>
Object.assign({}, S, {
type: (properties: any) =>
Object.assign(S.type(properties), { properties })
});
return <S extends URIS>(S: Schemable<S>) => {
let a = obj(partSchemable(S)) as Kind<S, T> & { properties?: any };
return "properties" in a ? S.partial(a.properties) : a;
};
}
它产生了一个新的 Schemable
跟踪 properties
对象传递到其 type()
方法并将其作为属性添加到结果中。那么,如果顶级结果a
有这样一个properties
属性(property),我们返回S.partial(a.properties)
放在上面而不是 a
.
我们可以按如下方式测试它的工作原理。首先我将增强someType
使用嵌套对象,这样我们就可以向自己证明只有顶级 S.type()
转换为 S.partial()
:
const someType = S.make(S =>
S.type({
id: S.string,
foo: S.string,
bar: S.boolean,
baz: S.type({ a: S.string })
})
);
然后我会做 myPartialType
(这就是您所说的 partial
):
const myPartialType = S.make(S => S.intersection(partial(someType)(S), propsBase(S)));
最后是一个测试函数(使用 log()
函数将内容放入我将在底部链接的 stackblitz 代码中的浏览器窗口中):
const test = (x: any) => {
log(JSON.stringify(x), myPartialType(G.guard).is(x));
};
test({ foo: "" }); // false
test({ id: "", foo: "" }); // true
test({ id: "", foo: "", bar: "" }); // false
test({ id: "", foo: "", bar: false }); // true
test({ id: "", baz: {} }); // false
test({ id: "", baz: { a: "" } }); // true
所以,这些有效。耶?我猜。
我的问题是:你有多需要这个?给出您的示例代码以及需要做什么才能开始接近通用partial()
,我强烈建议首先制作模式的两个版本,如下所示:
const props = <S extends URIS>(S: Schemable<S>) => ({
id: S.string,
foo: S.string,
bar: S.boolean,
baz: S.type({ a: S.string })
});
const someTypeRequired = S.make(S => S.type(props(S)));
const someTypePartial = S.make(S => S.partial(props(S)));
const easierPartialType = S.make(S =>
S.intersection(someTypePartial(S), propsBase(S))
);
在这里,我们保留了 properties
我们自己反对,这样我们就可以在以后使用它两次,而不是在扔掉它后需要重新恢复它。它产生与以前相同的结果:
easierTest({ foo: "" }); // false
easierTest({ id: "", foo: "" }); // true
easierTest({ id: "", foo: "", bar: "" }); // false
easierTest({ id: "", foo: "", bar: false }); // true
easierTest({ id: "", baz: {} }); // false
easierTest({ id: "", baz: { a: "" } }); // true
但这个版本不太容易出错。
好的,希望有帮助;祝你好运!
关于typescript - A 部分 部分 io-ts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61311182/
我是一名优秀的程序员,十分优秀!