microsoft / microsoft/TypeScript
satisfies operator shadows object this with interface this
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
🔍 Search Terms
I have been experimenting with the satisfies operator which has been pretty great to avoid having to interface anything. Instead, you can pass around the original object literal with all of its properties clearly shown in code completion.
But. Recently, I noticed there’s a slight problem. I am building this library where I’ve advocated for objects instead of classes or interfaces, everything working perfectly when they are just static objects. Yet when I want to add class-type semantics, namely pass an options and instantiated state that’s available to its functions, I face some difficulties.
This is how my interface looks:
export interface Extension<O = undefined, S = undefined> {
name: string;
opts?: O;
store?: S;
create?: (opts: O) => this & {
opts: O;
store: S;
};
onMount?: (editor: Editor, opts: O, store: S) => void;
onDestroy?: (editor: Editor, opts: O, store: S) => void;
}
// and use it as:
export const paragraph = {
name: 'paragraph' as const,
create(opts) {
this.opts = opts
this.store = createStore(opts)
return { ...this, opts, store: this.store }
}
} satisfies Extension<Options, Store>
Which does work and compile. Except when I access the object from the create function, I notice that the return type of create is actually no more the this of the original object but the this of the Extension interface.
So while the static paragraph object literal is neatly described with its original shape, if I create it with the create function the signature is altered and it then becomes Extension with no type inference about it properties.
At the moment, I am bypassing this by manually augmenting the exposed global property with the return type I'd want from create:
declare module '@library/core' {
interface Extensions {
paragraph: typeof paragraph & { opts: Options, store: Store }
// doesn't work
//paragraph: ReturnType<typeof paragraph.create>
}
}
✅ 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 isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
My request and general inquiry is, whether it is possible to use the original this—which is available if I remove the satisfies operator—for the create function? Even if I use a custom function eg make(opts: Options) { return this } it always inherits the Extension this even though it's not even defined in the interface which seems wrong. Why does the Extension this overshadow the original object literal this?
In short my suggestion is: satisfies operator should not overload/shadow the original object this inside functions outside the satisfied interface. Although shadowing this inside the interface functions suck as well but probably can't be prevented.
📃 Motivating Example
No more classes! TypeScript satisfies operator makes class-style inheritance and composition magically easy as the object this is no longer shadowed by the satisfied interface in the object functions.
💻 Use Cases
- What do you want to use this for?
- For making my object literal fully compatible with classes but much clearer and composable
- What shortcomings exist with current approaches?
- Loss of type inference when using
satisfies
- What workarounds are you using in the meantime?
- I augment the object literal manually:
paragraph: typeof paragraph & { opts: Optins, store: Store }
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 reproduire l’exemple motivant de satisfies et comparez le this inféré dans create avec le type inféré lorsque satisfies est supprimé. Suivez la manière dont l’interface Extension satisfaite fournit le contexte de la fonction. Le travail est terminé lorsqu’il est établi s’il est possible de préserver le this du littéral d’objet d’origine et que le comportement de typage résultant ou les contraintes de conception sont documentés.
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
- 30/100