microsoft / microsoft/TypeScript
Instance member function becomes instance member property in mapped types
Open
Nobody has claimed this yet.
Needs Investigation
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 4.0.0-dev.20200512
Search Terms:
- instance member function becomes instance member property
- mixin property of exported class expression may not be private or protected
Code
export class Application {
protected getState() {
return 'starting';
}
component(componentCtor: typeof Object) {
// mount the component
}
}
// Ideally, I'd like to write `function repositoryMixin(superClass: typeof Application)`, but that
// triggers "property of exported class expression may not be private or protected"
export function repositoryMixin<T extends MixinTarget<Application>>(superClass: T) {
return class extends superClass {
component(componentCtor: typeof Object) {
super.component(componentCtor);
// register repositories contributed by the component
}
}
}
// helpers to work around TypeScript limitations related to mixins
export type Constructor<T extends object> = new (...args: any[]) => T;
export type MixinTarget<T extends object> = Constructor<
{
// Enumerate only public members to avoid the following compiler error:
// Property '(some-name)' of exported class expression
// may not be private or protected. ts(4094)
[p in keyof T]: T[p];
}
>;
Expected behavior:
The code compiles cleanly.
Actual behavior:
$ tsc mixin-bug.ts
mixin-bug.ts:13:5 - error TS2425: Class '{ component: (componentCtor: ObjectConstructor) => void; }' defines instance member property 'component', but extended class '(Anonymous class)' defines it as instance member function.
13 component(componentCtor: typeof Object) {
~~~~~~~~~
mixin-bug.ts:14:13 - error TS2340: Only public and protected methods of the base class are accessible via the 'super' keyword.
14 super.component(componentCtor);
~~~~~~~~~
Found 2 errors.
Playground Link:
Related Issues:
- Remove property-override-method error #34494
- 3.7: instance member function becomes instance member property in type references [outcome: fixed since 3.7.0-dev.20191016] #33747
- Support for all type features in declaration files. #35822
- False error TS4094: ... exported class expression may not be private or protected. #30355
- Allow .d.ts files to represent anonymous class expressions with
privatemembers #36060 - Unexpected TS4094 with the build parameter
declaration: true#17293 - Generating type definitions for mixin classes with protected members #17744
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
Reproduce the reported behavior in mixin-bug.ts using the linked TypeScript Playground example, then compare the TS2425 and TS2340 diagnostics with the expected clean compilation. Investigate the compiler paths handling mapped types, mixin class expressions, and method/property member classification; done means the example compiles without those errors.
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