提问人:user1790300 提问时间:10/16/2023 最后编辑:jonrsharpeuser1790300 更新时间:10/16/2023 访问量:36
具有任一返回类型的接收类型错误
Receiving type errors with Either return type
问:
我正在努力使用 TypeScript 和 fp-ts 重写一个较旧的应用程序。我创建了一个验证函数,但遇到了几个类型错误。我不确定问题可能出在哪里,或者为什么它采用返回值并试图转换为包装在 .Either
Either
- 为什么会出现类型错误?
- 如何修复或解决类型错误?
- 我应该在函数空间中进行哪些编码操作,以确保将来可以大大减少此问题?
const validateRegistrationForm = (user: User): Either<Error, User> => {
return E.tryCatch(
async () => {
const schema = Joi.object().keys({
userName: Joi.string().min(4).max(255).required(),
firstName: Joi.string().pattern(new RegExp(/^[a-z\d\-_\s]+$/i)).min(1).max(50).required(),
lastName: Joi.string().pattern(new RegExp(/^[a-z\d\-_\s]+$/i)).min(1).max(50).required(),
emailAddress: Joi.string().email().required(),
password: Joi.alternatives().try(Joi.string(), Joi.number()).required(),
confirmPassword: Joi.alternatives().try(Joi.string(), Joi.number()).disallow(Joi.ref('password')).required(),
profilePicUrl: Joi.string().uri({scheme: [/https?/]})
});
// If post exists, wrap it in a 'some', otherwise return 'none'
let {error} = await schema.validateAsync(user);
return error && error.length > 0 ? E.left({name: "ValidationError", message: error.message}) : E.right(user);
},
(reason: Error) => E.left(new Error(String(reason))) // convert reasons to Error objects for consistency
);
}
error TS2322: Type 'Either<Either<Error, never>, Promise<Right<User> | Left<{ name: string; message: any; }>>>' is not assignable to type 'Either<Error, User>'.
Type 'Left<Either<Error, never>>' is not assignable to type 'Either<Error, User>'.
Type 'Left<Either<Error, never>>' is not assignable to type 'Left<Error>'.
Type 'Either<Error, never>' is not assignable to type 'Error'.
Type 'Left<Error>' is missing the following properties from type 'Error': name, message
return E.tryCatch(
答: 暂无答案
评论