microsoft / microsoft/TypeScript
Generating type definitions for mixin classes with protected members
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 2.4.2
Code:
I'm using mixins as described by: https://github.com/Microsoft/TypeScript/pull/13743
export type Constructor<T> = new(...args: any[]) => T;
export function Unsubscriber<T extends Constructor<{}>>(Base: T) {
class Unsubscriber extends Base implements OnDestroy {
protected unsubscribe: Subject<void> = new Subject();
ngOnDestroy() {
this.unsubscribe.next();
this.unsubscribe.complete();
}
}
return Unsubscriber;
}
If I compile this code with "declaration": true to get type definitions for my library, I get the following error:
ERROR in (truncated)/mixins.ts (...): Return type of exported function has or is using private name 'Unsubscriber'.
One solution is to add an interface...
export interface IUnsubscriber extends OnDestroy {
unsubscribe: Subject<void>;
}
...and have my mixin function have a return type of Constructor<IUnsubscriber>. This works, but it forces me to make the properties/methods exposed by my mixin be public even in cases where I want them to be protected.
Short of adding protected members to interfaces (which I'm not sure is the right thing to do), this seems to be a limitation of the currently supported mixin strategy.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the mixins.ts example in the issue and compile it with declaration generation enabled to reproduce the private-name error. Read the mixin and declaration-generation or type-checking paths involved, then verify that exported mixins with protected members produce usable type definitions without requiring those members to be public.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100