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: disallow object assignment to constructed types #2709

Open
Labels
accepting prsGo ahead, send a pull request that resolves this issueenhancement: new plugin ruleNew rule request for eslint-pluginpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin
@d-luk

Description

@d-luk

In TypeScript it's possible to use a class as a type/interface, as documented in theHandbook. I'm sure that there is use case for this, but in general this leads to unpredictable behaviour.

For example, take the following class:

classPoint{publicx:number;publicy:number;constructor(x:number,y:number){if(x<0||y<0){thrownewError('Point must have positive values');}this.x=x;this.y=y;}}

This constructor makes sure thatx andy cannot be negative values, however the following is allowed:

constmyPoint:Point={x:-1,y:-2};

This type of construction also breaksinstanceof checks:

constrealPoint=newPoint(1,2);console.log(realPointinstanceofPoint);// => trueconstfakePoint:Point={x:1,y:2};console.log(fakePointinstanceofPoint);// => false

Constructors are used to assert proper object instantiation. Therefore, allowing it to be circumvented can lead to bugs.

Proposed solution

Add a rule that enforces calling the constructor when initializing a class.

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-pluginpackage: 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