简体   繁体   English

TypeScript:类型参数不可分配

[英]TypeScript: Argument of type is not assignable

TypeScript newbie here migrating an existing project. TypeScript新手在这里迁移现有项目。 We have Mongoose models, which look similar to the following snippet and use the discriminatorKey property: 我们有猫鼬模型,其外观类似于以下代码片段,并使用discriminatorKey属性:

const task = new mongoose.Schema({
  name: {
    type: String
  },
  notes: {
    type: String
  }
}, {
  discriminatorKey: 'type',
  toObject: {
    virtuals: true
  },
  toJSON: {
    virtuals: true
  }
});

This gives me the following error during compilation: 这在编译期间给我以下错误:

src/models/task.ts(12,3): error TS2345: Argument of type '{ discriminatorKey: string; }' is not assignable to parameter of type 'SchemaOptions'.
  Object literal may only specify known properties, and 'discriminatorKey' does not exist in type 'SchemaOptions'.

I'm using these @types definition, which seems to be the most recent one: 我正在使用这些@types定义,这似乎是最新的定义:

"@types/mongoose": "^4.7.8"

I understand, that the type definitions do not specify the discriminatorKey (this is obviously visible when looking at node_modules/@types/mongoose/index.d.ts ), but I do not understand (a) why (oversight? different version? other reason?), (b) how can I circumvent this error? 我知道类型定义没有指定discriminatorKey (在查看node_modules/@types/mongoose/index.d.ts时显然可见),但是我不理解(a)为什么(监督?其他版本?其他)原因?),(b)如何避免该错误?

(c) Bonus question: The versioning strategy of the @types definitions is still unclear to me. (c)额外的问题: @types定义的版本控制策略对我来说仍然不清楚。 I would assume, that the type definitions should match the version of the actual library, however, often there does not seem to be a matching version -- eg we're using express version. 我认为类型定义应该与实际库的版本匹配,但是,通常似乎没有匹配的版本-例如,我们使用的是express版本。 4.13.4, but there is no matching @types/express version available. 4.13.4,但没有匹配的@types/express版本。 What's the best practice to follow in this case? 在这种情况下,最佳做法是什么?

I'm a bit late, but I have just come across the same issue. 我有点迟了,但是我遇到了同样的问题。 I'm using @types/mongoose 4.7.12 and it hasn't been updated yet. 我正在使用@types/mongoose 4.7.12并且尚未更新。

As a temporal solutions, when you come across this kind of issues, you have two options: 作为临时解决方案,当您遇到此类问题时,有两种选择:

🤦 Cast the whole problematic object to any 🤦将整个问题对象投射到any对象

As you are defining the options inline, you can cast them to any : 内联定义选项时,可以将它们强制转换为any

const task: mongoose.Schema = new mongoose.Schema({ ... }, {
    discriminatorKey: 'type',

    ...
} as any);

or 要么

const task: mongoose.Schema = new mongoose.Schema({ ... }, <any> {
    discriminatorKey: 'type',

    ...
});

The downside of this approach is that now type checking for that SchemaOptions object will be disabled for all the properties, so if you make a typo in, let's say, toJSON , and type toJSNO instead, TypeScript will not warn you. 这种方法的缺点是,现在将对所有属性禁用对该SchemaOptions对象的类型检查,因此,如果您键入toJSON ,而键入toJSNO ,则TypeScript将不会警告您。

👌 Cast the problematic object to any just in the statement that uses unknown properties 👌将有问题的对象强制转换为使用未知属性的语句中的any对象

A different approach to keep type checking working for the known properties but disable it for the yet unknown ones is to define that options object before with the known properties and add the unknown ones later casting them to any : 一种不同的方法,以保持类型检查的已知特性工作,但禁用它的未知的人是定义选项之前,对象与已知的特性,并添加未知的以后他们铸造any

const options: mongoose.SchemaOptions = {
    toObject: { virtuals: true },
    toJSON: { virtuals: true },
};

(options as any).discriminatorKey = 'type';

// or (<any> options).discriminatorKey = 'type';

const task = new mongoose.Schema({ ... }, options);

🙏 Side note: Please, contribute! 🙏旁注:请贡献!

If you find any similar issue in this package or in any other in the future, this is a really easy and minimal change that you can fix and PR to DefinitelyTyped . 如果您在此软件包或以后的任何其他软件包中发现任何类似的问题,则这是一个非常简单且最小的更改,您可以修复并将PR更改为DefinitelyTyped Actually, for something like this you don't even need to clone the repo, I was able to do it just by using the edit button in GitHub. 实际上,对于这样的事情,您甚至不需要克隆存储库,我只需要使用GitHub中的“编辑”按钮就可以做到。

Here's the PR: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/16598 , once it is merged, just update to the most recent version. 这是PR: https : //github.com/DefinitelyTyped/DefinitelyTyped/pull/16598 ,一旦合并,只需更新到最新版本即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决 - 类型参数不可分配给类型 - TYPESCRIPT - How to resolve - Argument of type is not assignable to type - TYPESCRIPT Typescript:a 类型的参数不能分配给 b 类型的参数 - Typescript: argument of type a is not assignable to parameter of type b Typescript 错误“类型参数不可分配给类型参数” - Typescript error 'argument of type is not assignable to parameter of type' typescript:类型&#39;any []&#39;的参数不能分配给&#39;[]&#39;类型的参数.ts(2345) - typescript : Argument of type 'any[]' is not assignable to parameter of type '[]'.ts(2345) TypeScript:&#39;Card | 类型的参数 undefined&#39; 不可分配给类型为 &#39;Card&#39; 的参数 - TypeScript: Argument of type 'Card | undefined' is not assignable to parameter of type 'Card' Typescript - 'string | 类型的参数 (string | null)[]' 不能分配给“string”类型的参数 - Typescript - Argument of type 'string | (string | null)[]' is not assignable to parameter of type 'string' 'string | 类型的参数 undefined' 不能分配给'string' 类型的参数。 typescript '严格' - Argument of type 'string | undefined' is not assignable to parameter of type 'string'. typescript 'strict' React 组件上的 Typescript 错误:“元素”类型的参数不可分配给类型的参数 - Typescript error on React Component: Argument of type 'Element' is not assignable to parameter of type React Typescript:'{ [x: number]: any; 类型的参数 }' 不可分配给类型的参数 - React Typescript: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type 打字稿键盘事件:“事件”类型的参数不能分配给“键盘事件”类型的参数 - Typescript keyboard event : argument of type 'Event' is not assignable to parameter of type 'KeyboardEvent'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM