microsoft / microsoft/TypeScript
Disallowing `throw`ing expressions that aren't assignable to `Error`
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
L’issue non fornisce alcun percorso di file o di test. Inizia individuando il controllo dei tipi delle istruzioni throw nel compilatore TypeScript e nei relativi test, quindi esamina come vengono gestite le espressioni nullable e any. Il lavoro è completo quando le espressioni elencate che non sono espressioni Error vengono rifiutate, mentre le espressioni Error e any rimangono consentite, senza modificare il JavaScript emesso.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100