microsoft / microsoft/TypeScript
Allow composite values to be used in type predicates
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
Allow composite values to be used in type predicates
đ Search Terms
Is, composite type, complex type, composite value, complex value, type predicate, type guard
â 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
Type predicates allow you to define functions that can tell the TypeScript compiler if a parameter is of a certain type.
I suggest that this be extended to allow type predicates to narrow the type of composite objects involving parameters.
That is, the left-hand-side of is types should be allowed to include object literals.
đ Example
Say you have a type that involves a subset of another type:
type MyType = {property1: "static text", property2: "Hello" | "World"};
A type predicate for this would always require passing in the property1 property, even though itâs unnecessary.
Now, you can create a type predicate that only takes the value of property2, but still gurantees the result type:
function foo(text: string): {property1: "static text", property2: text} is MyType {
return ["Hello", "World"].includes(text);
};
Iâve run into the problem of being unable to do this while developing my website.
I had a tagged union:
type A = {t: "A", value: "X" | "Y"};
type B = {t: "B", value: string};
type U = A | B;
And I wanted to get a value of type A from user input, but default back to a B type if the value wasnât
To do this, I hoped to be able to do this:
function zit(x: string): {t: "A", value: x} is A {
return ["X", "Y"].includes(x);
};
function gob(x: string): U {
return zit(x) ? {t: "A", value: x} : {t: "B", value: x};
};
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
Aucun fichier, test ou point dâentrĂ©e nâest nommĂ©. Commencez par examiner les exemples de prĂ©dicats de type de lâissue et le comportement existant du compilateur concernant les gardes de type ; la modification serait complĂšte lorsque les types dâobjet composites seront acceptĂ©s dans les prĂ©dicats et que les scĂ©narios de narrowing prĂ©sentĂ©s fonctionneront sans modifier le JavaScript gĂ©nĂ©rĂ©.
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
- 35/100