【发布时间】:2022-11-17 05:35:33
【问题描述】:
我认为 t.strict 会抛出不严格遵守定义的对象,但令我惊讶的是,t.strict 中未定义的额外属性仍然通过验证。有人知道t.type 和t.strict 之间的区别吗?
【问题讨论】:
标签: typescript
我认为 t.strict 会抛出不严格遵守定义的对象,但令我惊讶的是,t.strict 中未定义的额外属性仍然通过验证。有人知道t.type 和t.strict 之间的区别吗?
【问题讨论】:
标签: typescript
这与您可以验证的内容无关,而是您最终得到的运行时对象:
import * as T from "io-ts";
const Foo = T.type({a: T.number, b: T.string})
const Bar = T.strict({a: T.number, b: T.string})
const foo = Foo.decode({a: 100, b: "", c: true})
const bar = Bar.decode({a: 100, b: "", c: true})
console.log(foo);
console.log(bar); // No "c" property here
【讨论】: