Skip to content
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

strictPropertyInitialization should consider return #27555

Open
ghost opened this issue Oct 4, 2018 · 3 comments
Open

strictPropertyInitialization should consider return #27555

ghost opened this issue Oct 4, 2018 · 3 comments
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@ghost
Copy link

ghost commented Oct 4, 2018

TypeScript Version: 3.2.0-dev.20181004

Code

class C {
    x: number;

    constructor(b: boolean) {
        if (b) {
            this.x = 3;
        } else {
            return new C(true);
        }
    }
}

Expected behavior:

No error.

Actual behavior:

src/a.ts:2:5 - error TS2564: Property 'x' has no initializer and is not definitely assigned in the constructor.

2     x: number;
@ghost ghost added the Bug A bug in TypeScript label Oct 4, 2018
@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Bug A bug in TypeScript labels Oct 10, 2018
@RyanCavanaugh
Copy link
Member

We have no way to distinguish this correct code from the incorrect version that calls new C(false);

@ghost
Copy link
Author

ghost commented Oct 10, 2018

That would be an infinite recursion, which we normally allow and doesn't seem related to --strictPropertyInitialization.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Experience Enhancement Noncontroversial enhancements and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Oct 10, 2018
@RyanCavanaugh RyanCavanaugh added this to the Future milestone Oct 10, 2018
@RyanCavanaugh RyanCavanaugh reopened this Oct 10, 2018
@weswigham weswigham removed the Suggestion An idea for TypeScript label Nov 6, 2018
@RyanCavanaugh RyanCavanaugh added the Suggestion An idea for TypeScript label Mar 7, 2019
@AlCalzone
Copy link
Contributor

I have another case of this. I want to enforce that whenever a class instance is created, the correct sub class constructor is used. A simplified repro would be the following:

class Foo {
  constructor() {
    if (new.target !== Sub) return new Sub();
    this.version = 1;
  }

  public version: number;
  // Error: Property 'version' has no initializer and is not definitely assigned in the constructor.
}

class Sub extends Foo { } // default constructor has the same issue as any constructor which calls super

console.log(new Foo().version); // prints 1
console.log(new Sub().version); // prints 1

As you can see, version is always defined, but TypeScript doesn't think so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants