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

Generic type on class prevents access of protected entries with explicitly typed 'this' function argument #35067

Closed
adekau opened this issue Nov 12, 2019 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@adekau
Copy link

adekau commented Nov 12, 2019

TypeScript Version: Tested on 3.2.4, 3.6.3, 3.8.0-dev.20191112

Search Terms: Property, protected, accessible, through, instance, generics, decorator

Code

class Accessor<T> {
    private _properties: { [key: string]: any } = {};

    constructor() { }

    protected _get(key: string): any {
        return this._properties[key];
    }

    protected _set(key: string, value: any): void {
        this._properties[key] = value;
    }
}

function property() {
    return function (target: any, key: string): void {
        const getter = function (this: Accessor<any>) { return this._get('something')}
    };
}

Expected behavior: Should allow access to protected function _get since this is typed as an instance of the class Accessor.

Actual behavior:

index.ts:17:69 - error TS2446: Property '_get' is protected and only accessible through an instance of class 'Accessor<any>'.

17         const getter = function (this: Accessor<any>) { return this._get('something')}
                                                                       ~~~~

Note: this does not occur without the generic type on Accessor. If it is just class Accessor with function (this: Accessor) there is no error on calling this._get('something').

Playground Link: Playground Link

Related Issues: #10302, Potentially a duplicate of #29132. This was supposedly fixed in #24645 however it is problematic with generics as mentioned above.

@andrewbranch
Copy link
Member

andrewbranch commented Dec 17, 2019

This is a better description of the problem, but it is indeed a duplicate of #29132.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Dec 17, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

No branches or pull requests

3 participants