【问题标题】:Yup context typescript error: Property 'originalValue' does not exist on type 'TestContext<AnyObject>'是的上下文打字稿错误:类型 \'TestContext<AnyObject>\' 上不存在属性 \'originalValue\'
【发布时间】:2022-12-23 23:49:06
【问题描述】:

是的,因为没有键入上下文值而导致 Typescript 错误:

   Property 'originalValue' does not exist on type 'TestContext<AnyObject>'.

使用 yup 函数测试时,我收到 originalValue 的打字稿错误。我应该输入什么上下文来消除这个错误?

.test('00s', 'not a valid number', (value, context) => {
  return context.originalValue.match(ssnRegex)
}),

【问题讨论】:

    标签: react-hook-form yup


    【解决方案1】:

    您可以通过使用扩展接口对上下文进行类型转换来解决问题,请参阅:https://github.com/DefinitelyTyped/DefinitelyTyped/issues/49512

    interface TestContextExtended {
      originalValue?: unknown;
    }
    

    在你的测试中:

    .test('00s', 'not a valid number', (value, context) => {
      const { originalValue } = cont as Yup.TestContext & TestContextExtended;
    
      if (typeof originalValue === 'string') {
        return originalValue.match(ssnRegex);
      }
      return false;
    }),
    

    【讨论】:

      猜你喜欢
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2018-11-30
      • 2017-03-02
      • 2021-09-07
      • 2017-11-25
      相关资源
      最近更新 更多