microsoft / microsoft/TypeScript

Support either type asserting `self` in classes, or properly ignoring workaround

Open
#39,864 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: classes Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

I currently have a scenario very similar to https://github.com/microsoft/TypeScript/issues/2000#issuecomment-546929745. While it would be fixed it TS natively supported overriding class methods (see that issue), I followed the outline of the linked comment, but am running into a small issue. This is not a huge blocker but rather a nice-to have to get proper type completion for my scenario.


export class WritableObjStream<T> extends (stream.Writable as any) {
    writableObjectMode: true

    write(object: T, cb?: (error: Error | null | undefined) => void): boolean {
        return super.write(...more details)
    }
}

Because we type assert the base class to any, TS has no knowledge of the superclass. I tried a few solutions, with errors included below:

(super as any).write() // 'super' must be followed by an argument list or member access.
let s: stream.Writable = undefined;
// @ts-ignore
s = super // Identifier expected.

return s.write() // 'super' must be followed by an argument list or member access, Expected 1-3 arguments, but got 0.

Luckily, it autocomplete is picking up on the proper type of s.write() now and giving me an argument error.
Also, from what I can gather super is treated as a keyword not an identifier, which results in the error above.

This may not be the most common of errors, but there may be a few scenarios/other use cases. I tried writing a few examples up but none made real-world sense besides this use-case of a workaround for overriding methods.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the class example in the issue and the linked TypeScript issue/comment describing the workaround. Investigate how TypeScript parses and type-checks super in this scenario, then establish whether type-asserting self should be supported or the workaround should be explicitly rejected. Done means the chosen behavior is documented by tests covering the shown examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.