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

Cannot use this to index private or protected properties #17362

Closed
Pauan opened this issue Jul 23, 2017 · 4 comments
Closed

Cannot use this to index private or protected properties #17362

Pauan opened this issue Jul 23, 2017 · 4 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@Pauan
Copy link

Pauan commented Jul 23, 2017

TypeScript Version: 2.4.1

Code

This works fine:

class Foo {
  _foo: number;

  foo(): this["_foo"] {
    return this._foo;
  }
}

This works fine:

class Foo {
  protected _foo: number;

  foo(): Foo["_foo"] {
    return this._foo;
  }
}

I expect this to work, but it does not work:

class Foo {
  protected _foo: number;

  foo(): this["_foo"] {
    return this._foo;
  }
}

It fails with this error:

(4,10): error TS2536: Type '"_foo"' cannot be used to index type 'this'.
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jul 24, 2017
@weswigham
Copy link
Member

weswigham commented Jul 24, 2017

I think this is expected given our rules on indexed access types - given that the indexed access operator is defined in terms of keyof T (see here) and keyof T is:

If T has no apparent string index signature, keyof T is a type of the form "p1" | "p2" | ... | "pX", where the string literals represent the names of the public properties of T. If T has no public properties, keyof T is the type never.

So this will require adding an exception whereby private names are actually accessible when indexing this.

@Pauan
Copy link
Author

Pauan commented Jul 25, 2017

@weswigham But Foo["_foo"] is the same situation, and it works fine.

So does indexing on classes have an exception for private/protected members, but that exception wasn't applied to this?

@mhegazy mhegazy added Design Limitation Constraints of the existing architecture prevent this from being fixed and removed Bug A bug in TypeScript labels Nov 9, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 9, 2017

As @weswigham noted, this is the intended behavior. it becomes very hard to reason about these types in higher order if you have to carry around a context to know if the name is visible or not.

@typescript-bot
Copy link
Collaborator

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

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

5 participants