Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Rule proposal: prevent array, iterable and function spreads into objects #748

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
@ckknight

Description

@ckknight

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

No one assigned

    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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp