microsoft / microsoft/TypeScript
Add support for type guarding to if statements
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
Suggestion
🔍 Search Terms
type guard guarding assertion
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
This proposal introduces a new feature that would allow developers to use type guard statements with if-statements rather than only with functions. This feature would provide a more concise and expressive syntax for type guards, making code easier to read and maintain.
📃 Motivating Example
With the proposed feature, developers could use the is keyword directly within an if-statement to perform a type guard. For example…
Before — requires separate function for type guarding
function isIterable<T>(obj: Iterable<T> | ArrayLike<T>): obj is Iterable<T> {
return typeof (obj as Iterable<T>)[Symbol.iterator] === 'function';
}
function iterateOver<T>(obj: Iterable<T> | ArrayLike<T>, expose: Function) {
if (isIterable(obj)) {
// some logic here
} else {
// some logic here
}
}
After — can handle type guarding inline via if statement
function iterateOver<T>(obj: Iterable<T> | ArrayLike<T>, expose: Function) {
const isIterable = typeof (obj as Iterable<T>)[Symbol.iterator] === 'function';
if (isIterable): obj is Iterable<T> {
// some logic here
} else {
// some logic here
}
}
** this could just as easily be rewritten to get rid of the isIterable placeholder altogether, like this:
function iterateOver<T>(obj: Iterable<T> | ArrayLike<T>, expose: Function) {
if (typeof (obj as Iterable<T>)[Symbol.iterator] === 'function'): obj is Iterable<T> {
// some logic here
} else {
// some logic here
}
}
💻 Use Cases
Currently, if developers want to use a type guard to narrow down the type of a value within an if statement, they need to define a separate function (AFAIK) that returns a boolean value indicating whether the value is of a certain type. This approach can be cumbersome and can lead to code that is harder to read and understand.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par les exemples motivants et le comportement existant des gardes de type décrit dans l’issue. Aucun fichier source, test ou point d’entrée du compilateur n’est nommé. Le travail serait considéré comme terminé lorsqu’une syntaxe et une sémantique pour les gardes de type inline auraient été définies, avec l’implémentation et la validation correspondantes.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript, typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100