简体   繁体   English

VSCode Eslint 错误 no-unsafe-assignment 显示不正确

[英]VSCode Eslint Error no-unsafe-assignment Incorrectly Displayed

This is a weird issue involving @typescript-eslint/no-unsafe-assignment.这是一个涉及@typescript-eslint/no-unsafe-assignment 的奇怪问题。 I had a typescript file, call it oldmodel.ts, and in my VSCode IDE, when I load the file, the following does NOT produce any error in my ESlint VSC terminal:我有一个 typescript 文件,称为 oldmodel.ts,在我的 VSCode IDE 中,当我加载文件时,以下内容不会在我的 ESlint VSC 终端中产生任何错误:

export interface ITestObject {
  createDate?: Date,
}

export class TestObject implements ITestObject {
  createDate?: Date;

  constructor(options: ITestObject = {}) {
    this.createDate = (options.createDate instanceof Date || !isNaN(Date.parse(options.createDate || ''))) ? new Date(options.createDate || '') : undefined;
  }
}

If I copy and paste this same exact code in a newly created newmodel.ts file, existing in the same folder as oldmodel.ts, my VSCode produces that unsafe-assignment error for the line beginning with this.createDate .如果我将相同的代码复制并粘贴到新创建的 newmodel.ts 文件中,该文件与 oldmodel.ts 位于同一文件夹中,我的 VSCode 会为以this.createDate开头的行生成不安全分配错误。 Does anybody know how/why this is happening?有人知道这是如何/为什么会发生的吗?

Ok, I cannot remember who suggested this since he deleted his comment, but I followed his suggestion and it seemed to work.好的,我不记得是谁提出了这个建议,因为他删除了他的评论,但我听从了他的建议,似乎奏效了。 So what I did was in the newmodel.ts file, I created another class that referenced the concerning class:所以我所做的是在 newmodel.ts 文件中,我创建了另一个 class,它引用了相关的 class:

interface ISampleResponse {
  testObject?: TestObject
}

export class SampleResponse implements ISampleResponse {
  testObject?: TestObject

  constructor(options: ISampleResponse = {}) {
    this.testObject = new TestObject(options.testObject);
  }
}

Then, in another file, say an injectable service for example, I proceeded to import that response class in the file:然后,在另一个文件中,例如一个可注入服务,我继续将该响应 class 导入文件中:

import { SampleResponse } from '../models/newmodel.ts';

After I saved that file, I went back to newmodel, went to the line that had that eslint error, modified it just so it could channel a change, and then the error disappeared.保存该文件后,我回到 newmodel,转到出现该 eslint 错误的行,对其进行修改以便它可以引导更改,然后错误消失了。 It may simply be that there has to be a reference made to that file with that class in order for the error to go away.可能只是必须使用该 class 对该文件进行引用,以消除 go 的错误。

I need to verify this a couple more times, but if it still works, I'll mark this as an answer for everyone to follow.我需要再验证几次,但如果它仍然有效,我会将其标记为每个人都可以遵循的答案。

I guess that until you don't declare your new component "TestObject" in your appropiate module, the file code could show some errors sometimes.我想,除非您不在适当的模块中声明新组件“TestObject”,否则文件代码有时可能会显示一些错误。

Can you test if when you add "TestObject" to the declaration section of themodule where is the new file, the errors disappear?您能否测试当您将“TestObject”添加到新文件所在模块的声明部分时,错误是否消失?

EDIT: I could think in 2 different causes/reasons:编辑:我可以考虑 2 个不同的原因/原因:

  1. The oldmodel file name was "test-object.ts" and match with the class name ("TestObject"), but the new file (new-object.ts") doesn't match with "TestObject".旧模型文件名为“test-object.ts”,与 class 名称(“TestObject”)匹配,但新文件(new-object.ts)与“TestObject”不匹配。

  2. You are calling the oldmodel as TestObject, and the new one as well.您将旧模型称为 TestObject,也将新模型称为 TestObject。 Then, the angular system "suffer" a problem, so it is showing that error.然后,angular 系统“遇到”问题,因此显示该错误。

Does any of this new one answers make sense to you?这个新答案中的任何一个对您有意义吗? Could you test it?你能测试一下吗?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM