microsoft / microsoft/TypeScript
Type inference fails to recognize never when it's the result of a template tag function
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
🔎 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
💻 Code
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit der bereitgestellten Playground-Reproduktion und vergleichen Sie die Behandlung des Kontrollflusses für den gewöhnlichen Aufruf, der nie zurückkehrt, mit der Invocation des getaggten Template-Literals. Verfolgen Sie den Typprüfpfad für getaggte Template-Literale und überprüfen Sie, dass ein Tag, der nie zurückkehrt, den Kontrollfluss als beendet behandelt, einschließlich des im Bericht gezeigten Falls einer unerreichbaren Rückgabe.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100