microsoft / microsoft/TypeScript
Disallowing `throw`ing expressions that aren't assignable to `Error`
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
throw, throwing non-error
Suggestion
Add typechecking to throw statement, allowing it to only throw Errors and anys.
Code currently allowed, but with strong potential for bugs:
throw null;
throw undefined;
throw 'foobar';
throw '';
throw 42;
throw {foo: 'bar'};
throw potentiallyNullable; // with –strictNullChecks
After implementing this feature request, such code wouldn't be allowed.
Use Cases
Even if it's possible to throw non-errors in JavaScript code, it's extremely confusing and hard to debug. Failing to properly handle such cases happened even to Mozilla.
Examples
Forbidden code:
throw null;
throw undefined;
throw 'foobar';
throw 42;
throw {foo: 'bar'};
throw potentiallyNullable; // with –strictNullChecks
Allowed:
throw new Error();
throw null as any;
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code - it can break existing TypeScript code.
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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
Das Issue nennt keine Datei und keinen Testpfad. Beginne damit, die Typprüfung von throw-Anweisungen im TypeScript-Compiler und dessen Tests zu lokalisieren, und prüfe anschließend, wie nullable- und any-Ausdrücke behandelt werden. Als erledigt gilt die Änderung, wenn die aufgeführten Ausdrücke, die keine Error-Ausdrücke sind, abgelehnt werden, während Error- und any-Ausdrücke weiterhin zulässig bleiben, ohne das erzeugte JavaScript zu ändern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100