denoland / deno_lint

Blazing fast linter for JavaScript and TypeScript written in Rust
https://lint.deno.land/
MIT License
1.51k stars 163 forks source link

constructor overload of derived class should allow custom logic around calling super. #1272

Open sylc opened 1 month ago

sylc commented 1 month ago

Lint Name

deno-lint(constructor-super)

Code Snippet

class Base {
    #name: string
    #index: number
    constructor(foo: { name: string, index: number }) {
        this.#name = foo.name
        this.#index = foo.index
    }
    print() {
        console.log(`${this.#name} - ${this.#index}`)
    }
}

class Derived extends Base {
    constructor(foo: string)
    constructor(foo: { name: string, index: number } | string) {
     if (typeof foo === "string") {
        super({ name: foo, index: 0 })
     } else {
        super(foo)
     }
    }
}

new Derived('hello').print()

Expected Result

No warning. (the ts playground site is happy with it as well)

Actual Result

image

Additional Info

Version

deno 1.43.1 (release, x86_64-pc-windows-msvc) v8 12.4.254.12 typescript 5.4.3

workaround

As i was writing this I found a workaround (apart from disabling the lint)

class Derived extends Base {
    constructor(foo: string)
    constructor(foo: { name: string, index: number } | string) {
     let arg: { name: string, index: number } | string;
      if (typeof foo === "string") {
        arg = { name: foo, index: 0 }
     } else {
        arg = foo
     }
     super(arg)
    }
}