Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Closed
Labels
accepting prsGo ahead, send a pull request that resolves this issueenhancement: new plugin ruleNew rule request for eslint-pluginlocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin
Description
Repro
// Array spreadconstsquares=[1,4,9,16];constcubes=[1,8,27];constbothArray={ ...squares, ...cubes};// rule would error here, as spreading an array causes surprising resultsbothArray==={0:1,1:8,2:27,3:16}// TS: typeof bothArray === (typeof Array<number>) ❌// Iterable spreadconstmap=newMap([[1,2],[3,4]]);constset=newSet([5,6,7,8]);constbothIterable={ ...map, ...set};// rule would error here as spreading an iterable does nothingbothIterable==={}// TS: typeof allFunc === (typeof Set<number> & typeof Map<number, number>) ❌// Function spreadfunctionfuncDecl(){}constfuncExpr=function(){};constarrowFunc=()=>{};constallFunc={ ...funcDecl, ...funcExpr, ...arrowFunc};// rule would error here as spreading a function does nothingallFunc==={};// TS: typeof allFunc === ({}) ✅// Object with call-signatureinterfaceFuncWithProps{property?:string;():number;}constfuncWithProps:FuncWithProps=()=>1;funcWithProps.property='foo';constspreadFuncWithProps={...funcWithProps};// rule would NOT error here as custom function props are spread as expectedspreadFuncWithProps==={property:'foo'};// TS: typeof allFunc === (FuncWithProps) ✅
TypeScript does not have an understanding of how iterable object spreads work, and borks the types.
This leads to unsoundness and broken code.
TypeScript does understand function object spreads, but they are noops, so they shouldn't be allowed.
Additional Information
Detecting this should be a matter of detecting whether the type of each expression being spread within anobject implementsIterable<T>
, defined withinlib.es2015.iterable.d.ts
.
The converse rule of preventing objects being spread within an array seems unnecessary/out-of-scope, given that the TypeScript type system is able to determine that each expression spread within an array is iterable.
Metadata
Metadata
Assignees
Labels
accepting prsGo ahead, send a pull request that resolves this issueenhancement: new plugin ruleNew rule request for eslint-pluginlocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin