Skip to content

Allow type assertion for "super" keyword in method. #41034

Open

Description

@whzx5byb

Search Terms

super type assertion

Suggestion

Currently there is no way to provide a type assertion for "super" keyword, because the "super" keyword cannot be wrapped by parentheses, so either (<T>super).x or (super as T).x will result in an error: "'super' must be followed by an argument list or member access.ts(1034)".

The parentheses wrapping "super" which only for type assertion use should be allowed.

Use Cases

class A {
  // a decorator will add a instance method "foo" here.
}

class B extends A {
  bar() {
    (<A & {foo: () => void}>super).foo(); // doesn't work.
  }
}

class C extends A {
  bar() {
    (<A & {foo: () => void}>A.prototype).foo(); // this works.
  }
}

Checklist

My suggestion meets these guidelines:

  • [●] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [●] This wouldn't change the runtime behavior of existing JavaScript code
  • [●] This could be implemented without emitting different JS based on the types of the expressions
  • [●] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [●] This feature would agree with the rest of TypeScript's Design Goals.

Activity

  1. whzx5byb commented on Oct 10, 2020

    @whzx5byb
    Author

    That code would result in invalid JS.

    Unless TS removes the wrapping parenthesis, (super)() is invalid.

    TS do removes the wrapping parenthesis.
    (this as any).x(); will compile to this.x(); link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions