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

error TS4020: 'extends' clause of exported class 'SampleNewClass' has or is using private name 'SampleMixin' #15870

Closed
khusamov opened this issue May 16, 2017 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@khusamov
Copy link

khusamov commented May 16, 2017

TypeScript Version: 2.3.2

Code

File SampleMixin.ts with mixin class:

export type Constructor<T = object> = new(...args: any[]) => T;

export interface SampleMixin {
    pluginManager: string[];
}

export default <S extends Constructor>(Superclass: S): Constructor<SampleMixin> & S => class extends Superclass {
    
    private _pluginManager: string[];
    
    get pluginManager(): string[] {
		return this._pluginManager ? this._pluginManager : this._pluginManager = [];
	}
	
}

The file index.ts where this mixin is used:

import SampleMixin from './SampleMixin';

class SampleSuperClass {}

export default class SampleNewClass extends SampleMixin(SampleSuperClass) {}

Expected behavior:

Compilation complete. Watching for file changes.

Actual behavior:

index.ts(6,22): error TS4093: 'extends' clause of exported class 'SampleNewClass' refers to a type whose name cannot be referenced.
index.ts(6,45): error TS4020: 'extends' clause of exported class 'SampleNewClass' has or is using private name 'SampleMixin'.
@mhegazy mhegazy added the Bug A bug in TypeScript label May 16, 2017
@mhegazy mhegazy added this to the TypeScript 2.4 milestone May 16, 2017
@sandersn sandersn added this to Not started in Rolling Work Tracking May 16, 2017
@sandersn sandersn moved this from Not started to In Progress in Rolling Work Tracking Jun 6, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Jun 14, 2017

This looks like the same underling issue as #9944. the rule is if the compiler tries to write the type, and it needs an import from another module, and it was not already available, then error..

if you were to write the type manually for this class, you would have needed something like:

import SampleMixin, { SampleMixin as SampleMixinType, Constructor } from './b'; 

class SampleSuperClass { }

const base: Constructor<SampleMixinType> & typeof SampleSuperClass = SampleMixin(SampleSuperClass);

export default class SampleNewClass extends base { }

since both SampleMixinType and Constructor are not already imported, the compiler is giving you this error. the fix is to ensure they are imported, and/or you write an explicit type annotation for the base.

#9944 tracks adding imports automatically in such cases.

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels Jun 14, 2017
@mhegazy mhegazy removed this from the TypeScript 2.4.1 milestone Jun 14, 2017
@sandersn sandersn moved this from In Progress to Done in Rolling Work Tracking Jun 14, 2017
@sandersn sandersn removed this from Done in Rolling Work Tracking Jun 14, 2017
@rohmanhm
Copy link

rohmanhm commented Jul 7, 2017

styled-components/styled-components#973

@mhegazy
Copy link
Contributor

mhegazy commented Aug 17, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants