Skip to content

error TS2612 Property will overwrite base property not raised if "strictNullCheck" is true #48667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
amodiopescefaro opened this issue Apr 13, 2022 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@amodiopescefaro
Copy link

Bug Report

If you enable useDefineForClassFields then a new error is possible TS26126

If you have code that trigger TS2612 Property 'xxx' will overwrite the base propert in 'yyy'. It this is intentional, add an initializer. Othertwise, add a "declare" modifier or remove the redundant declaration.

The error do not appear if you enable strictNullCheck.

Without strictNullCheck you'll see this error.

If you put strict=true or strictNullCheck=true the error is not reported anymore

🔎 Search Terms

TS2612
strictNullCheck

🕗 Version & Regression Information

Noticed this on typescript 4.6.3
Tested on typescript@nightly 4.7.0-dev.20220408 and it behave the same (no error on strict mode)
Tested on 4.3.5 and it behave the same (no error on strict mode)

TS2612 was introduced in 4.3 so previous version do not make sense

⏯ Playground Link

This link contain a minimal code to show the problem but you need to enable/disable strictNullCheck and useDefineForClassFields to see it in action

Playground link with relevant code

💻 Code

type Base = {
    base: string;
};

type Derived = Base & {
    derived: string;
}

class A {
    prop: Base;

    constructor(prop?: Base) {
        this.prop = prop ?? { base: "base" };
    }
}

class B extends A {
    // This should trigger TS2612 but does not do it with strictNullCheck enabled
    prop: Derived;

    constructor(prop: Derived) {
        super();
        this.prop = prop;
    }
}

🙁 Actual behavior

When compiled with:

  • target - esnext
  • useDefineConfigForClass - true
  • strictNullCheck - true

the code does not raise any error

🙂 Expected behavior

It should raise the TS2612 error

@andrewbranch
Copy link
Member

This is by design:

Note that the compiler can only check constructors for initialisation when strictNullChecks: true. I chose to be conservative and always issue the error for strictNullChecks: false.

@sandersn in #33509

The experience does seem a little unfortunate, but I don’t have a better idea off the top of my head.

@andrewbranch andrewbranch added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Apr 21, 2022
@sandersn
Copy link
Member

Specifically, B.prop has an initialiser: in B's constructor, this.prop = prop. This means that the compiler thinks your overwrite of the base property is intentional.

@iamgabrielsoft
Copy link

iamgabrielsoft commented Nov 15, 2022

or you could change it to es2015 or es2020, I think it is by design for esnext

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

4 participants