首页 > 解决方案 > typescript @types/yup 验证返回未定义?

问题描述

我在我的打字稿项目中使用 yup 已经有一段时间了。它需要 @types/yup 来定义类型。我正在使用v0.26.4它,一切都很好。但是今天我将依赖项更新为v0.29.3. 这里不对劲:

//The object schema constructor from 
<T extends object>(fields?: ObjectSchemaDefinition<T>): ObjectSchema<T>;

//changed to 
<T extends object>(fields?: ObjectSchemaDefinition<T>): ObjectSchema<T | undefined>;

//then for my validation code:
const validated = await CreateNewsValidator.validate(req.body); // T | undefined
//when i use validated, the error Type 'undefined' is not assignable to type X comes

我的问题是验证方法何时返回未定义?什么是解决方法,我应该切换回 0.26.4 吗?他们为什么要做出这样的改变?

标签: typescripttypesyup

解决方案


推荐阅读