Skip to main content

no-misused-spread

Disallow using the spread operator when it might cause unexpected behavior.

🔒

ESLint 配置 中扩展"plugin:@typescript-eslint/strict-type-checked" 可启用此规则。

💡

此规则报告的一些问题可以通过编辑器 建议 手动修复。

💭

该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。

扩展语法 (...) 是一种 JavaScript 功能,用于创建具有一个或多个其他对象的连接属性的对象。TypeScript 允许传播其属性通常不打算枚举的对象,例如数组和类实例。

¥Spread syntax (...) is a JavaScript feature for creating an object with the joined properties of one or more other objects. TypeScript allows spreading objects whose properties are not typically meant to be enumerated, such as arrays and class instances.

此规则不允许对类型表明这样做可能会导致意外行为的值使用扩展语法。这包括以下情况:

¥This rule disallows using the spread syntax on values whose types indicate doing so may cause unexpected behavior. That includes the following cases:

  • Promise 展开到对象中。你可能打算将其 await

    ¥Spreading a Promise into an object. You probably meant to await it.

  • 将没有属性的函数展开到对象中。你可能打算调用它。

    ¥Spreading a function without properties into an object. You probably meant to call it.

  • 将可迭代对象(ArrayMap 等)展开到对象中。可迭代对象通常没有有意义的可枚举属性,你可能打算将其分散到数组中。

    ¥Spreading an iterable (Array, Map, etc.) into an object. Iterable objects usually do not have meaningful enumerable properties and you probably meant to spread it into an array instead.

  • 将字符串展开到数组中。JavaScript 中围绕编码字符的字符串枚举行为通常令人惊讶。

    ¥Spreading a string into an array. String enumeration behaviors in JavaScript around encoded characters are often surprising.

  • class 展开到对象中。这会复制类的所有静态自有属性,但不复制任何继承链。

    ¥Spreading a class into an object. This copies all static own properties of the class, but none of the inheritance chain.

  • 将类实例展开到对象中。这不会忠实地复制实例,因为只复制了它自己的属性,但继承链会丢失,包括它的所有方法。

    ¥Spreading a class instance into an object. This does not faithfully copy the instance because only its own properties are copied, but the inheritance chain is lost, including all its methods.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-misused-spread": "error"
}
});

在线运行试试这个规则 ↗

示例

¥Examples

declare const promise: Promise<number>;
const spreadPromise = { ...promise };

declare function getObject(): Record<string, strings>;
const getObjectSpread = { ...getObject };

declare const map: Map<string, number>;
const mapSpread = { ...map };

declare const userName: string;
const characters = [...userName];
Open in Playground
declare class Box {
value: number;
}
const boxSpread = { ...Box };

declare const instance: Box;
const instanceSpread = { ...instance };
Open in Playground

选项

该规则接受以下选项:

type Options = [
{
/** An array of type specifiers that are known to be safe to spread. */
allow?: (
| {
from: 'file';
name: [string, ...string[]] | string;
path?: string;
}
| {
from: 'lib';
name: [string, ...string[]] | string;
}
| {
from: 'package';
name: [string, ...string[]] | string;
package: string;
}
| string
)[];
},
];

const defaultOptions: Options = [{ allow: [] }];

¥Options

allow

An array of type specifiers that are known to be safe to spread. Default: [].

此选项采用共享 TypeOrValueSpecifier 格式

¥This option takes the shared TypeOrValueSpecifier format.

file.ts 文件中此选项的配置示例:

¥Examples of a configuration for this option in a file.ts file:

"@typescript-eslint/no-misused-spread": [
"error",
{
"allow": [
{ "from": "file", "name": "BrandedString", "path": "file.ts" },
]
}
]
declare const unbrandedString: string;

const spreadUnbrandedString = [...unbrandedString];
Open in Playground

何时不使用它

¥When Not To Use It

如果你的应用有意以不寻常的方式处理原始数据,例如直接操作类原型链,则你可能不需要此规则。

¥If your application intentionally works with raw data in unusual ways, such as directly manipulating class prototype chains, you might not want this rule.

如果你对不寻常扩展的使用案例仅涉及几种类型,你可以考虑使用 ESLint 禁用注释 和/或 allow 选项,而不是完全禁用此规则。

¥If your use cases for unusual spreads only involve a few types, you might consider using ESLint disable comments and/or the allow option instead of completely disabling this rule.

进一步阅读

¥Further Reading


Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.

See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.

'## 资源'