You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
abstractclassTestInterface<P>{constructor(protectedfoo: P){}publicabstracttrace(): string;}functionTraceInterfaceA<P>(this: TestInterface<P>): string{return`${this.foo}`;// Property 'foo' is protected and only accessible through an instance of class 'TestInterface<P>'.ts(2446)}functionTraceInterfaceB<P>(this: TestInterface<P>): string{return`<pre>${this.foo}</pre>`;// Property 'foo' is protected and only accessible through an instance of class 'TestInterface<P>'.ts(2446)}functiongetTestInterfaceInstance(strategy: 'A'|'B'){returnclassStrategyTestInterface<P>extendsTestInterface<P>{publictrace=strategy==='A' ? TraceA : TraceB;}}
Expected behavior:
If I'm not wrong, with this argument on Function, we allow only to access to public attributes, don't we? So we are supposed to get an error on the Test class implementation.
Or if the latest decision is to allow access to protected attributes, so we aren't supposed to received an error in the TestInterface example. Actual behavior:
Without the interface, we can access to protected attributes, but when an interface is provided to the generic Class definition, we lose the ability to reach protected properties.
Playground Link: TSPlayground Related Issues: #12510 Cannot access protected members when specifying 'this' type #17694 Accessing private method with this argument specifier
The text was updated successfully, but these errors were encountered:
As the #10302 suggests and how it's implemented in #24645 , I think the good implementation is to allow access only to protected and public methods and members (but not static) in the this overload of a function.
So the second part, with Abstract is where there is a bug imho.
However, this also happen when using intersection type and union type:
If I remove generics from the Wrapper class, it works. But once I add a generic type, it shows the error: Property 'getValue' is protected and only accessible through an instance of class 'Wrapper<any>'
TypeScript Version: 3.2.2
Search Terms:
function this parameter class interface protected property
Code
Expected behavior:
If I'm not wrong, with
this
argument onFunction
, we allow only to access to public attributes, don't we? So we are supposed to get an error on theTest
class implementation.Or if the latest decision is to allow access to protected attributes, so we aren't supposed to received an error in the
TestInterface
example.Actual behavior:
Without the interface, we can access to protected attributes, but when an interface is provided to the generic Class definition, we lose the ability to reach protected properties.
Playground Link:
TSPlayground
Related Issues:
#12510 Cannot access protected members when specifying 'this' type
#17694 Accessing private method with
this
argument specifierThe text was updated successfully, but these errors were encountered: