【发布时间】:2018-04-04 21:07:30
【问题描述】:
我创建了一个 NPM 模块,其中包含一些 Typescript 类型...现在我正在尝试在另一个应用程序中使用此模块,但遇到了一些问题!这可能会让人感到困惑,因此我将尝试通过省略与问题无关的属性来简化示例...
在模块中,我的 index.d.ts 文件中有以下内容:
import * as Joi from 'joi';
import { RequestHandler } from 'express';
export interface ISteadyOptions {
customTypes?: IParamType[],
middleware?: (RequestHandler|ErrorRequestHandler)[],
}
export interface IParamType {
name: string
validator: (param: object) => Joi.ArraySchema
}
同时,在我的应用程序中,我有这样的东西:
const opts: ISteadyOptions = {
middleware: [
logger('dev'),
express.static('images')
],
customTypes: [
{
name: "point",
validator: function(param) {
return Joi.array().items(
config.getAvailableGroups()
)
}
}
]
};
我的想法是我只能将 Express 请求处理程序添加到 middleware 数组中,并且我可以定义一个带有 Joi 架构的 customType(例如 point),以便在需要时运行。
问题 1 - 我可以在 opts.middleware 里面放任何东西
例如,TypeScript 说这是完全有效的:
middleware: [
logger('dev'),
express.static('images'),
"not a request handler"
]
如果我在 opts 对象的其他地方添加了其他无效的内容,那么当 VSCode 给我错误时,我可以看到由于某种原因 middleware 的类型已更改为 any[]。
我不明白为什么会发生这种情况?
问题 2 - 我的 validator 函数无法正常工作
我收到此错误:
Types of property 'customTypes' are incompatible.
Type '({ name: string; description: string; validator: (param: object) => ArraySchema; example: string[...' is not assignable to type 'IParamType[]'.
Type '{ name: string; description: string; validator: (param: object) => ArraySchema; example: string[]...' is not assignable to type 'IParamType'.
Type '{ name: string; description: string; validator: (param: object) => ArraySchema; example: string[]...' is not assignable to type 'IParamType'.
Types of property 'validator' are incompatible.
Type '(param: object) => ArraySchema' is not assignable to type '(param: object) => ArraySchema'. Two different types with this name exist, but they are unrelated.
Type 'ArraySchema' is not assignable to type 'ArraySchema'. Two different types with this name exist, but they are unrelated.
Types of property 'validate' are incompatible.
Type '{ <T>(value: T): ValidationResult<T>; <T>(value: T, options: ValidationOptions): ValidationResult...' is not assignable to type '{ <T>(value: T): ValidationResult<T>; <T>(value: T, options: ValidationOptions): ValidationResult...'. Two different types with this name exist, but they are unrelated.
Type 'ValidationResult<any>' is not assignable to type 'ValidationResult<any>'. Two different types with this name exist, but they are unrelated.
Types of property 'error' are incompatible.
Type 'ValidationError' is not assignable to type 'ValidationError'. Two different types with this name exist, but they are unrelated.
Types of property 'details' are incompatible.
Type 'ValidationErrorItem[]' is not assignable to type 'ValidationErrorItem[]'. Two different types with this name exist, but they are unrelated.
Type 'ValidationErrorItem' is not assignable to type 'ValidationErrorItem'. Two different types with this name exist, but they are unrelated.
Types of property 'path' are incompatible.
Type 'string[]' is not assignable to type string.
我能够复制我的用例(如下所示),并且按预期工作......我显然做错了什么......但我不知道是什么?!
import { RequestHandler, ErrorRequestHandler } from 'express';
import * as Joi from 'joi';
interface IValidator {
validator: (data: string) => Joi.AnySchema
}
const thing: IValidator = {
validator: data => {
return Joi.string()
}
}
// this works
console.log(
thing.validator("javascript")
);
interface IMiddleware {
middleware: (RequestHandler|ErrorRequestHandler)[]
}
const thing2: IMiddleware = {
middleware: [
(res, req, next) => {
return next()
},
"foo" // this causes an error
]
}
【问题讨论】:
标签: node.js typescript express joi