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

Merged external modules break protected member access checking #6731

Closed
masaeedu opened this issue Jan 29, 2016 · 4 comments · Fixed by #6736
Closed

Merged external modules break protected member access checking #6731

masaeedu opened this issue Jan 29, 2016 · 4 comments · Fixed by #6736
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@masaeedu
Copy link
Contributor

Having some difficulty utilizing the external module augmentations feature merged in #6213. Here is a test case to demonstrate the problem:

// a.ts
export class A {
    protected protected: any;

    public setProtected(val: any) {
        this.protected = val;
    }
}
// b.ts
import {A} from './a'; // Only here to make b.ts an external module

declare module "./a" {
    interface A { }
}

This results in the following error:

a.ts(5,9): error TS2446: Property 'protected' is protected and only accessible through an instance of class 'A'.

It looks like the problem is that checkClassPropertyAccess in checker.ts ends up obtaining the merged interface as the enclosing class of the property access, rather than the actual enclosing class, then tries to ensure that this interface is derived from the class which declares the property, which isn't the case.

@vladima
Copy link
Contributor

vladima commented Jan 29, 2016

this is not specific to module augmentations and can be reproduced with two script files:

// file1.ts
class A {
    protected protected: any;

    public setProtected(val: any) {
        this.protected = val;
    }
}
// file2.ts
interface A { }

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 29, 2016
@masaeedu
Copy link
Contributor Author

@vladima Not sure if this is related, but overrides of protected members are still broken:

// a.ts
export class A {
    protected protected: any;

    protected setProtected(val: any) {
        this.protected = val;
    }
}
// b.ts
import {A} from './a';

declare module "./a" {
    interface A { }
}

export class B extends A {
    protected setProtected() {

    }
}

This results in:

b.ts(7,14): error TS2415: Class 'B' incorrectly extends base class 'A'. Property 'setProtected' is protected but type 'B' is not a class derived from 'A'.

@vladima
Copy link
Contributor

vladima commented Jan 31, 2016

the crux of the problem is the same but in different part of the checker - have a fix for it.
Note: it is also can be reproduced without augmentations

@vladima
Copy link
Contributor

vladima commented Jan 31, 2016

filed #6762 to track it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants