microsoft / microsoft/TypeScript
Surprising `Not all code paths return a value`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
TypeScript Version: 2.5.2
Code
type AsyncFunction<T> = () => Promise<T>;
const f: AsyncFunction<number | void> = async () => {
if (Math.random()) {
return 1;
}
};
Expected behavior:
No tsc errors.
Actual behavior:
const f: AsyncFunction<number | void> = async () => {
// ~~~~~~~~~~~~~ [ts] Not all code paths return a value.
if (Math.random()) {
return 1;
}
};
Notably, when using return-type annotation, this error does not occur:
async function a(): Promise<number | void> {
if (Math.random()) {
return 1;
}
}
const b = async (): Promise<number | void> => {
if (Math.random()) {
return 1;
}
};
In our real-world scenario, it would not be sufficient to look for this syntactic pattern. Rather, the fact that the async function is assigned to a union containing void should be the trigger that silences this error.
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
Reproduziere die TypeScript 2.5.2-Beispiele mit tsc und vergleiche die asynchrone Pfeilfunktion, die AsyncFunction<number | void> zugewiesen ist, mit den explizit annotierten Funktionen. Abgeschlossen ist die Aufgabe, wenn die Union, die void enthält, die unerwartete Diagnose unterdrückt, während der Compiler weiterhin tatsächlich unvollständige Rückgabepfade meldet.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100