文档管理中心
您当前浏览的HarmonyOS 5.0.1(API 13)文档归档不再维护,推荐您使用最新版本。详细请参考文档维护策略变更
指南DevEco Studio附录Code Linter代码检查规则通用规则@typescript-eslint@typescript-eslint/no-unsafe-argument

@typescript-eslint/no-unsafe-argument

不允许将any类型的值作为函数的参数传入。

该规则仅支持对.js/.ts文件进行检查。

规则配置

收起
自动换行
深色代码主题
复制
  1. // code-linter.json5
  2. {
  3. "rules": {
  4. "@typescript-eslint/no-unsafe-argument": "error"
  5. }
  6. }

选项

该规则无需配置额外选项。

正例

收起
自动换行
深色代码主题
复制
  1. declare function foo(arg1: string, arg2: number, arg3: string): void;
  2. foo('a', Number.MAX_VALUE, 'b');
  3. const tuple1 = ['a', Number.MAX_VALUE, 'b'] as const;
  4. foo(...tuple1);
  5. declare function bar(arg1: string, arg2: number, ...rest: readonly string[]): void;
  6. const array: string[] = ['a'];
  7. bar('a', Number.MAX_VALUE, ...array);
  8. declare function baz(arg1: Readonly<Set<string>>, arg2: Readonly<Map<string, string>>): void;
  9. baz(new Set<string>(), new Map<string, string>());

反例

收起
自动换行
深色代码主题
复制
  1. declare function foo(arg1: string, arg2: number, arg3: string): void;
  2. const anyTyped = Number.MAX_VALUE as any;
  3. // 变量anyTyped是any类型,不允许作为参数传入函数中
  4. foo(...anyTyped);
  5. // 变量anyTyped是any类型,不允许作为参数传入函数中
  6. foo(anyTyped, Number.MAX_VALUE, 'a');
  7. const anyArray: any[] = [];
  8. // 变量anyArray是any类型数组,不允许将数组元素作为参数传入函数中
  9. foo(...anyArray);
  10. const tuple1 = ['a', anyTyped, 'b'] as const;
  11. // 变量anyTyped是any类型数组,不允许将数组元素作为参数传入函数中
  12. foo(...tuple1);

规则集

收起
自动换行
深色代码主题
复制
  1. plugin:@typescript-eslint/recommended
  2. plugin:@typescript-eslint/all

Code Linter代码检查规则的配置指导请参考代码Code Linter检查

在 指南 中进行搜索
请输入您想要搜索的关键词