microsoft / microsoft/TypeScript

`// @ts-mutate` decorator/directive

Ouverte
#61,179 13 commentaires 2 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

🔍 Search Terms

mutate type, alter type, post-declaration narrow

✅ Viability Checklist
⭐ Suggestion

When doing property assignment to a variable already typed, allow a // @ts-mutate above the assignment which will

  • A) Not throw an error trying to assign to a non-existent property
  • B) Further narrow the type, encompassing the change

Example:

interface ObjectType {
  bar: string
}

const foo: ObjectType = { bar: 'hello' }

// @ts-mutate
foo.baz = 'hello'

foo
// ^? -- this is now `ObjectType & { baz: string }`
📃 Motivating Example

One specific example of why this would be useful would be a query constructor for type inference. There are GraphQL query constructors out there (GenQL) that query by supplying an object. I've built functions that yield reusable queries but sometimes I want to extend them.

This would allow me to take an existing query object and add an additional field to query, keeping my strict type inference.

đŸ’» Use Cases
  1. What do you want to use this for?

This could be used for constructing large objects sequentially and with inference without having to basically duplicate the information

  1. What shortcomings exist with current approaches?

If you have an existing variable, you have to alter its type declaration with the type that it is (explicitly) and intersect it with your changes. This also doesn't necessarily narrow correctly. There's no exisiting clean, easy way to do this that works off of type inference and narrowing

  1. What workarounds are you using in the meantime?
     // UGLY
     const query: ReturnType<typeof queryProp> & { prop: { addition?: true } } = queryProp(...args)
     query.prop.addition = true

Guide de contribution

Ouvrir le guide de contribution

Par oĂč commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela Ă©vite que deux personnes fassent le mĂȘme travail.
  3. Forkez le dépÎt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

L’issue n’identifie pas les fichiers d’implĂ©mentation, les tests ni les points d’entrĂ©e. Commencez par examiner le comportement proposĂ© de @ts-mutate et son interaction avec la vĂ©rification des affectations et le rĂ©trĂ©cissement des types ; le travail n’est terminĂ© que lorsque la conception est acceptĂ©e et que le comportement d’infĂ©rence demandĂ© est couvert par des tests.

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é
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.