Skip to content
← Back to rules

typescript/no-misused-spread Correctness

This rule is turned on by default when type-aware linting is enabled.
💭 This rule requires type information.
💡 A suggestion is available for this rule.

它的作用

此规则不允许在不合理或可能导致运行时错误的地方使用展开语法。

这为什么不好?

展开运算符可能会被以不太明显的方式误用,从而导致运行时错误或意外行为。此规则有助于捕获常见的误用。

示例

此规则的错误代码示例:

ts
// 在数组中展开一个不可迭代的值
const num = 42;
const arr = [...num]; // 运行时错误:num 不可迭代

// 在数组中展开一个 Promise
const promise = Promise.resolve([1, 2, 3]);
const arr2 = [...promise]; // 运行时错误:Promise 不可迭代

// 在对象字面量中展开非对象
const str = "hello";
const obj = { ...str }; // 创建 { '0': 'h', '1': 'e', ... },这可能出乎意料

此规则的正确代码示例:

ts
// 展开数组
const arr1 = [1, 2, 3];
const arr2 = [...arr1];

// 展开对象
const obj1 = { a: 1, b: 2 };
const obj2 = { ...obj1 };

// 展开已解析的 Promise
const promise = Promise.resolve([1, 2, 3]);
const arr3 = [...(await promise)];

// 如有需要,对不可迭代值使用 Array.from
const str = "hello";
const arr4 = Array.from(str); // ['h', 'e', 'l', 'l', 'o']

配置

此规则接受一个包含以下属性的配置对象:

allow

type: array

default: []

一个允许被展开的类型或值说明符数组, 即使它们通常会被标记为误用。

allow[n]

type: object | string

用于匹配特定声明的类型或值说明符

支持四种说明符类型:

  1. 字符串说明符(已弃用):按名称进行通配匹配
json
"Promise"
  1. 文件说明符:匹配在本地文件中声明的类型/值
json
{ "from": "file", "name": "MyType" }
{ "from": "file", "name": ["Type1", "Type2"] }
{ "from": "file", "name": "MyType", "path": "./types.ts" }
  1. 库说明符:匹配 TypeScript 内置库类型
json
{ "from": "lib", "name": "Promise" }
{ "from": "lib", "name": ["Promise", "PromiseLike"] }
  1. 包说明符:匹配 npm 包中的类型/值
json
{ "from": "package", "name": "Observable", "package": "rxjs" }
{ "from": "package", "name": ["Observable", "Subject"], "package": "rxjs" }
allow[n].from

type: "file"

必须为 "file"

allow[n].name

type: array | string

要匹配的类型或值的名称

名称说明符,可以是单个字符串或字符串数组

allow[n].name[n]

type: string

allow[n].path

type: string

用于指定类型或值必须声明于何处的可选文件路径。 如果省略,则会匹配所有文件。

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "options": {
    "typeAware": true
  },
  "rules": {
    "typescript/no-misused-spread": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  options: { typeAware: true },
  rules: {
    "typescript/no-misused-spread": "error",
  },
});
bash
oxlint --type-aware --deny typescript/no-misused-spread

版本

此规则在 v1.12.0 中加入。

参考资料