【问题标题】:Types being coerced to "any" and "Two different types with this name exist, but they are unrelated" error类型被强制为“任何”和“存在具有此名称的两种不同类型,但它们不相关”错误
【发布时间】: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


    【解决方案1】:

    我不确定第一个问题。但我会使用RequestHandlerParamsArray&lt;RequestHandler | ErrorRequestHandler&gt; 作为类型。

    关于第二个问题。您使用的类型与定义的类型不同。不确定您传递给 customTypes 的类型,但 { name: string; description: string; validator: (param: object) =&gt; ArraySchema; example: string[... 与您拥有的给定声明不匹配。所以不确定你是如何输入传入数据的。

    (param: object) =&gt; ArraySchema' is not assignable to type '(param: object) =&gt; ArraySchema 是由两种不同的类型引起的,但它们的名称相同。所以你在一个地方做import {ArraySchema} from 'joi';,在另一个地方做import {ArraySchema} from 'different-lib';。导致类型冲突。这适用于您遇到的几个重复类型错误。

    【讨论】:

    • 问题 2 是因为我在不同的项目上安装了不同版本的 Joi!
    猜你喜欢
    • 1970-01-01
    • 2019-02-21
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    相关资源
    最近更新 更多