microsoft / microsoft/TypeScript

Type inference fails to recognize never when it's the result of a template tag function

オープン
#61,039 コメント 4 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

### 🔎 Search Terms

tagged template literal never type

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about the never type and about template literals.

### ⏯ Playground Link

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABMAhjANgFQKYFsAO6KU2AFAM5QBOMYA5uQFyI4FEkDK1tDAglVRQBPADSIAdJIBuKdCGxNEKMEIDaAXQCUzMNinYqiAN4AoROcRQAFlTgB3RLocBRAXCqkA5KgytCxbCV0O2FySxt7ck9NAG4TAF8TE1BIWARkNHQAMXBoeDBSbUc9A2MzC2tbBydEV1sPb0yc1PygkKEwysjouMSTAHp+xABhdypsaHQhRHGIODowGAAvBXDAlLz0iFl0RAATOFWwOCgZ7CgQKjBk3LSkKCF8bGGrCYBrA1GBCagpgBkYB9yJgrDByKQUFQ6AB+ZiUGj0IrwnhlCyIGDARAQqGaVFoizjC5XJRQuJoxJonzZW75Qq9JKDRAASUgYx+U2QRAYazORPuj0Cdhg1mYzU2SCIEDeYWwYD2KMJlyQlACuFlp2Ue15SssAv2hzCx1OtAgcj2gU84HNwFo2D2nnEpAATABmABsbs0Nxa6QeTxe7wMLLm30mQgAIgaAHInAEfEFg7EwuHcREphF0PHmDFYyF0XGmfEE846vNkiwUixUvzsbAAAwARig9nX6QMhl9Zr9puRsKtrMQeb25nLtcSVSQ1WBjWFwOMUBArCgG+hsN7xbr-a8pZ82WHIwoY1A49gE+C87DEMi01fU5nCxYc0mC+Ui4riWXX4hK+YqWK7nSX7vkgnivOg6BwJ4baMlkmSdHAV59uEg7WIEw4IFqwG3qq6rorOYDzouy6ruudybs824fFQwZ7t2J7AqC55Qpe175umKIPtmmLPlm+JYZ+5JftWeD+CQjbNq2QElsSoHYOBkH0kAA

### 💻 Code

```ts
function failTemplate(strings: TemplateStringsArray, ...values: any[]): never {
throw new Error('failTemplate always throws');
}

function failFunction(): never {
throw new Error('failFunction always throws');
}

// Correctly recognizes the function call does not return
function typeCheckerCorrectlyLikesThis(arg?: string): string {
if (arg) {
return arg;
}
failFunction();
}

// Incorrectly flags the return type with: Function lacks ending return statement and return type does not include 'undefined'.(2366)
function typeCheckerIncorrectlyDoesNotLikeThis(arg?: string): string {
if (arg) {
return arg;
}
failTemplate`bad`;
}

// Correctly sees that the second return statement is unreachable
function typeCheckerCorrectlyDoesNotLikeThis(arg?: string): string {
if (arg) {
return arg;
}
failFunction();
return 'hello';
}

// Fails to see that the second return statement is unreachable
function typeCheckerIncorrectlyLikesThis(arg?: string): string {
if (arg) {
return arg;
}
failTemplate`bad`;
return 'hello';
}
```

### 🙁 Actual behavior

The invocation of a template tag function (via a tagged template literal) that is declared to return type `never` terminates the flow of control of the containing function, but the type checker does not recognize this and complains as if execution had continued beyond the invocation.

It appears, to my naive sensibilities, that the use of a tagged template literal is not recognized as a function invocation even though it is one. However, if the return type of the tag function is, say, `number`, the type inference engine _does_ correctly recognize this, so it seems like the issue is specifically with the flow control logic associated with `never`.

### 🙂 Expected behavior

A template literal using a `never` returning template tag function should be recognized the same as an ordinary invocation of a `never` returning function.

### Additional information about the issue

_No response_

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

提供されている Playground の再現から始め、通常の決して戻らない呼び出しとタグ付きテンプレート呼び出しに対する制御フローの扱いを比較してください。タグ付きテンプレートリテラルの型チェック経路を追跡し、決して戻らないタグが制御フローを終了させるものとして扱われることを、レポートに示されている到達不能な return のケースも含めて確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。