A 部分 部分 io-ts

jam*_*mid 2 types typescript

我在使用时遇到一些问题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) // needs to be partial,
  propsBase(S)
))

// So that this returns `true`
partial(G.guard).is({
  id: 'some id',
  bar: true
})
Run Code Online (Sandbox Code Playgroud)

但是someType(S)将类型定义为具有所需的所有键,但不应需要任何键。

这让我发疯,任何帮助将不胜感激!

jca*_*alz 5

我不是 io-ts 方面的专家,但更仔细地观察这一点,我认为这不是一件容易实现的事情。一个中的方法Schemable从其他架构中构建架构。该type()方法采用一个properties对象,该对象的键是所需对象的键,并且其属性对于每个属性都是可架构的。该partial()方法采用相同的properties对象,但构建一个可架构,其中属性是可选的。您可以看到这些如何允许您构建架构;你有一个properties包含你需要测试的所有键的对象。

但给定一个可架构的 for type T,通常不可能将其转换为可架构的 for Partial<T>properties传入的对象在type()其结果中不可访问。它已经丢失了;扔掉。例如,如果您拥有的只是一个类型保护函数,true当且仅当给定 type 的值时才返回T,则您不能使用该函数来创建另一个true当且仅当给定 type 的值时才生成的函数Partial<T>。您需要T来自守卫功能之外的某些来源的更多详细信息,例如properties现在消失的对象。

因此,您无法直接组合架构来完成此任务。


做到这一点的唯一方法是编写您自己的Schemable存储有关调用它的足够信息Schema,以便它可以返回一个新模式,其中,例如,对 的顶级调用S.type()被对 的调用替换S.partial()。如果您想查看跨并集或向下到交叉点的部分分布,即使这样也不是完美的。本质上,它必须对 执行手术Schema,解包一个顶级调用S.type()以获取丢失的properties对象,然后用 重新包装它S.partial()

制作一个将所有调用转换为S.type()into的方法很容易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;
  };
}
Run Code Online (Sandbox Code Playgroud)

它创建一个 newSchemable来跟踪properties传递到其type()方法中的对象并将其作为属性添加到结果中。然后,如果顶级结果a具有这样的properties属性,我们将返回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 })
  })
);
Run Code Online (Sandbox Code Playgroud)

然后我会做myPartialType(这就是你所说的partial):

const myPartialType = S.make(S => S.intersection(partial(someType)(S), propsBase(S)));
Run Code Online (Sandbox Code Playgroud)

最后是一个测试函数(使用一个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
Run Code Online (Sandbox Code Playgroud)

所以,这些有效。耶?我猜。


我的问题是:你有多需要这个?鉴于您的示例代码以及开始接近通用目的需要做什么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))
);
Run Code Online (Sandbox Code Playgroud)

在这里,我们自己保留了该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
Run Code Online (Sandbox Code Playgroud)

但这个版本不太容易出错。


好的,希望有帮助;祝你好运!

Stackblitz 代码链接