microsoft / microsoft/TypeScript
Support `!==` operator in `is` and `asserts` return type syntax
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
🔍 Search Terms
unknown, assert, narrow, undefined, "is not"
✅ Viability Checklist
- 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 our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
This looks like something that would have been asked before, but it's hard to search for.
Right now asserts is a pretty magical syntax that lets you do:
function assert(proposition: unknown): asserts proposition {
if (!proposition) {
throw new Error()
}
}
function foo(value: unknown): {} | null {
assert(value !== undefined);
return value;
}
However, trying to express a more specialized function doesn't work
function assertNotUndefined<T>(value: T): asserts value is Exclude<T, undefined> {
if (value === undefined) {
throw new Error();
}
}
function foo(value: unknown): {} | null {
assertNotUndefined(value);
return value; // Doesn't work
}
This is because Exclude<unknown, undefined> is still just unknown, since unknown is not just an alias for {} | null | undefined. Assuming that won't change, how else can one express assertNotUndefined? Well, if we take the original assert function and try to retroactively rationalize its syntax as this
function assert(proposition: unknown): asserts proposition;
when called as
assert(proposition !== undefined)
(hand-waving) just specializes to this
function assert(proposition: unknown): asserts proposition !== undefined;
I don't care to bikeshed over whether the syntax should be asserts proposition is not undefined, whether the LHS or RHS operands can be flipped, whether != or other operators should exist, or if any type expressions other than primitive literals like undefined should be supported, so I'll leave that to the comments.
📃 Motivating Example
function assertNotUndefined(proposition: unknown): asserts proposition !== undefined;
function foo(value: unknown): {} | null {
assertNotUndefined(value);
return value; // Works!
}
💻 Use Cases
- What do you want to use this for?
A function that can excludeundefinedcan be composed with other functions, e.g.array.filter(isNotUndefined) - What shortcomings exist with current approaches?
There's no clean way to define such a function without ugly conditionals to handleunknown. - What workarounds are you using in the meantime?
Rewrite code in more verbose style, e.g. manually construct a new array using aforloop instead ofarray.filter.
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 de l’issue et comparez la syntaxe d’assertion !== proposée avec la syntaxe existante des types de retour avec is et asserts. Le périmètre des opérateurs et des expressions de types pris en charge n’est volontairement pas défini ; la réalisation nécessiterait donc de trancher la syntaxe et la sémantique, d’implémenter le narrowing des types et d’ajouter une couverture appropriée du compilateur.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- 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