deno_lint icon indicating copy to clipboard operation
deno_lint copied to clipboard

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

Open sylc opened this issue 9 months ago • 0 comments

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)
    }
}

sylc avatar May 05 '24 06:05 sylc