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

Enhancement: [restrict-template-expressions] allow expressions withnever type #5325

Closed
Labels
accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin
@mikeyhew

Description

@mikeyhew

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://typescript-eslint.io/rules/restrict-template-expressions#allownullish

Description

The restrict-template-expressions rule currently complains if the template expression has typenever. This occurs, for example, if you have code like this:

// more variants may be added to Foo in the futuretypeFoo=|{type:"a",value:number};functioncheckFoosAreMatching(foo1:Foo,foo2:Foo){if(foo1.type!==foo2.type){// since Foo currently only has one variant, this code is never run,// and `foo1.type` has type `never`.thrownewError(`expected${foo1.type}, found${foo2.type}`);// ^ Invalid type "never" of template literal expression.}}

I don't see the benefit of the lint warning here, which complains thatfoo1.type andfoo2.type have typenever and says,Invalid type "never" of template literal expression.

I am suggesting changing the lint rule's behaviour to allow template expressions to have the typenever. This could be done as a new option calledallowNever, although I personally don't see the point in giving the option to disallownever in template expressions. Why should this lint rule be complaining about dead code?

Fail

constfoo:"a"|null;constbar:"a";if(foo!==bar){// `foo` has type `null`, which is not allowed// with the default options for this rulethrownewError(`${foo} and${bar}arenotequal);}

Pass

constfoo:"a";constbar:"a";if(foo!==bar){// `foo` and `bar` have type `never`, which should be allowed// by this rulethrownewError(`${foo} and${bar}arenotequal);}

Additional Info

This same logic should also apply to similar rules likerestrict-plus-operands (no need to complain that an operand has typenever), although I haven't checked to see how that rule behaves fornever operands.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: 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