Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a type for merging classes #588

Closed
ajvincent opened this issue Apr 8, 2023 · 3 comments
Closed

Add a type for merging classes #588

ajvincent opened this issue Apr 8, 2023 · 3 comments

Comments

@ajvincent
Copy link
Contributor

ajvincent commented Apr 8, 2023

TypeScript's documentation for class decorators makes one thing clear, at the very end of the section: any new class fields you define are not available after the decorator returns.

TypeScript's supported decorators as experimental for a while, but version 5.0 supports ECMAScript stage 3 decorators. For a while I've been wondering how to use a class decorator to build a subclass. Not having the type information is annoying.

I have a minimal fix, though.
https://github.com/ajvincent/es-membrane/blob/master/_01_stage_utilities/source/DecoratedClass.mts

With a simple test:
https://github.com/ajvincent/es-membrane/blob/master/_01_stage_utilities/spec/DecoratedClass.mts

I'm still working through the function code, but the DecoratedClass type works pretty well.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@ajvincent
Copy link
Contributor Author

ajvincent commented Apr 9, 2023

Hm, a better name for this might be MergeClass, as we can hit this without decorators. Also, because there's already Merge and Class types in type-fest.

@ajvincent ajvincent changed the title Add a type for decorated classes Add a type for merging classes Apr 9, 2023
@ajvincent
Copy link
Contributor Author

type MixinClass<
  AddedStatic,
  AddedInstance,
  BaseClass extends Class<unknown>,
  Arguments extends unknown[] = ConstructorParameters<BaseClass>,
> = Constructor<InstanceType<BaseClass> & AddedInstance, Arguments> & Omit<BaseClass, "prototype"> & AddedStatic;

@ajvincent
Copy link
Contributor Author

Duplicate of #274. But I can still write a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant