Error when calling super.method() on a method that is defined as an arrow function in Typescript

Viewed 213

When a method is defined in a base class as an arrow function, when calling that method in a derived class via super.method(), I get an error "Only public and protected methods of the base class are accessible via the 'super' keyword.ts(2340)", Even though the method was defined as public.

Screenshot

I understand that in an arrow function the context is lexical. This would affect the value of this and super in an arrow function, but in the image above Sun.met1 is not defined as an arrow function, so I don't see why it can't access Father.met.

1 Answers

You can fix this by changing the target in your tsconfig.json to from es5 to es6 or newer:

"target": "es6"

I'm not sure if this is by-design or not but have opened https://github.com/microsoft/TypeScript/issues/32121 to get clarification from the TS team

Alternatively, just use normal methods instead of arrow functions

Related