【问题标题】:What's the difference between `t.type` and `t.strict` in `io-ts`?`io-ts` 中的 `t.type` 和 `t.strict` 有什么区别?
【发布时间】:2022-11-17 05:35:33
【问题描述】:

我认为 t.strict 会抛出不严格遵守定义的对象,但令我惊讶的是,t.strict 中未定义的额外属性仍然通过验证。有人知道t.typet.strict 之间的区别吗?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    这与您可以验证的内容无关,而是您最终得到的运行时对象:

    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
    
    

    Blitz

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-22
      • 1970-01-01
      • 2020-03-16
      • 2011-08-23
      • 1970-01-01
      相关资源
      最近更新 更多