我有一个我创建的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
),并在需要时运行它。< / p>
问题1 - 我可以将任何内容放在opts.middleware
例如,TypeScript说这完全有效:
middleware: [
logger('dev'),
express.static('images'),
"not a request handler"
]
如果我在opts
对象的其他位置添加了其他无效的内容,那么当VSCode向我提供错误时,我可以看到由于某种原因middleware
的类型已更改为{{1} }。
我无法弄清楚为什么会发生这种情况?
问题2 - 我无法使any[]
功能正常工作
我收到此错误:
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.
答案 0 :(得分:1)
我不确定第一个问题。但我会使用RequestHandlerParams
或Array<RequestHandler | ErrorRequestHandler>
作为类型。
至于第二期。您使用的是与定义类型不同的类型。不确定要传递给customTypes的类型,但{ name: string; description: string; validator: (param: object) => ArraySchema; example: string[...
与您给定的声明不匹配。所以不确定如何输入传入的数据。
(param: object) => ArraySchema' is not assignable to type '(param: object) => ArraySchema
由两种不同类型引起,但它们的名称相同。因此,您在一个地方import {ArraySchema} from 'joi';
和另一个地方import {ArraySchema} from 'different-lib';
。导致类型冲突。这适用于您遇到的一些重复类型错误。