简体   繁体   English

Typescript 数组 Map 的对象不产生任何不安全的任何错误

[英]Typescript Array Map of Objects Producing No Unsafe Any Error

I'm in the process of converting my Angular project to v13 in a new workspace, and as I was moving code over, I came across a typescript-eslint error that I don't have an answer for.我正在将我的 Angular 项目转换为新工作区中的 v13,当我移动代码时,我遇到了一个我没有答案的 typescript-eslint 错误。

The code that worked before was as follows:之前工作的代码如下:

interface IConfigurationSetting {
  category?: string,
  key?: string,
  value?: string | number,
  message?: string
}

export class ConfigurationSetting implements IConfigurationSetting {
  category: string;
  key: string;
  value: string | number;
  message: string;

  constructor(options: IConfigurationSetting = {}) {
    this.category = options.category || '';
    this.key = options.key || '';
    this.value = options.value || '';
    this.message = options.message || '';
  }
}

export class ConfigurationSettingsGroup {
  settings: ConfigurationSetting[];
  errors: string[];

  constructor(options: {
    settings?: ConfigurationSetting[],
    errors?: string[]
  } = {}) {
    this.settings = (options.settings || []).map(setting => new ConfigurationSetting(setting));
    this.errors = options.errors || [];
  }
}

Now when the eslinter on VSC is finished, it produces an error for the setting parameter in the new ConfigurationSetting(setting) call - " Unsafe argument of type 'any' assigned to a parameter of type 'IConfigurationSetting' ".现在,当 VSC 上的 eslinter 完成时,它会在新的 ConfigurationSetting(setting) 调用中为设置参数产生错误 - “将‘any’类型的不安全参数分配给‘IConfigurationSetting’类型的参数”。

I build out my classes as such because I want to be sure that the properties of complex objects or array of objects have all the necessary defaults established.我这样构建我的类是因为我想确保复杂对象或对象数组的属性已建立所有必要的默认值。 Am I still allowed to map arrays of complex objects like above?我仍然允许像上面这样的复杂对象 map arrays 吗? If so, what can I do to have it bypass this unsafe rule without disabling it altogether?如果是这样,我该怎么做才能让它绕过这个不安全的规则而不完全禁用它? Or, is there now a better way to property map arrays of complex object types?或者,现在有没有更好的方法来属性 map arrays 复杂的 object 类型?

UPDATE: I made one change that removed the Unsafe argument of type 'any' assigned to a parameter of type 'IConfigurationSetting' error:更新:我做了一项更改,删除了分配给“IConfigurationSetting”类型参数的“任何”类型的不安全参数错误:

this.settings = <ConfigurationSetting[]>(options.settings || []).map((setting: ConfigurationSetting) => new ConfigurationSetting(setting));

But I am still getting a typescript-eslint error:但我仍然收到 typescript-eslint 错误:

Unsafe call of an `any' typed value.对“任何”类型值的不安全调用。 eslint(@typescript-eslint/no-unsafe-call) eslint(@typescript-eslint/no-unsafe-call)

Anybody can help me figure that out?任何人都可以帮我弄清楚吗?

There are a few implicit type conversions on your snippet, and that seems to be what the linter is warning about.您的代码片段中有一些隐式类型转换,这似乎是 linter 警告的内容。

These look like the cause:这些看起来像原因:

constructor(options: IConfigurationSetting = {}) {
                                         //   ^ here, this has "any" type
this.settings = (options.settings || []).ma...
                                  //  ^ here, this has "any[]" type

You could do some casting to work around it:你可以做一些铸造来解决它:

constructor(options: IConfigurationSetting = <IConfigurationSetting>{}) {
...

this.settings = (options.settings || <ConfigurationSetting[]>[]).ma...

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

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