TypeScript complain “has no initializer and is not definitely assigned in the constructor” about constructors by returning constructed object

后端 未结 4 2149
花落未央
花落未央 2021-02-20 03:19

TypeScript show following error message to this code samples:

class MyClass {
  someField: boolean;
  constructor() {
    return { someField: true };
  }
}


        
相关标签:
4条回答
  • 2021-02-20 03:40

    It considers {someField: true} as new object and the property someField has not been initialized.What is the purpose of return inside the constructor? You can replace it with this.someField = true.

    Edit: Actually, I debugged for more info, Try adding "strictPropertyInitialization": false to your compiler options and check. But it overrules the type strictness(beautiful feature of TS). But In my opinion, do not do this. For more info.

    0 讨论(0)
  • 2021-02-20 03:51

    Solution 1

    Add (!) sign after name:

    someField!:string;
    

    Solution 2

    Open TypeScript config file tsconfig.json and add this code to compiler options

     "angularCompilerOptions": {
        //   ...
        "strictPropertyInitialization": false
        //   ...
      }
    
    

    Note: it will make static analysis weaker

    0 讨论(0)
  • 2021-02-20 03:56

    strictPropertyInitialization forces you to initialize all properties that are not optional in the constructor of the class. This check can be useful as it ensures that you don't get unexpected uninitialized properties. There are several ways to get around the error, the first two are the general way to do it, in your case only the last on applies (I include all for completeness):

    Initialize the field

    If you define the property as boolean if should be true or false initialize it when you declare the field or initialize it in the constructor:

    class MyClass {
      someField: boolean = false;
      constructor() {
        return { someField: true };
      }
    }
    

    Make the field optional

    If the field can be undefined, you should mark this in the field declaration either by using ? or typing the field as undefined|boolean

    class MyClass {
        //someField?: boolean;
        someField: boolean | undefined;
        constructor() {
            return { someField: true };
        }
    }
    

    Use a not null assertion

    In your case since in the constructor you are actually not initializing the current object (this) but returning a new one, you can tell the compiler it is wrong about the error and use a not null assertion. This assertion is specifically introduced because there are limitations in strictPropertyInitialization checks and sometimes the compiler gets it wrong. For those cases you can override what the compiler thinks, but you have to be explicit about it:

    class MyClass {
        someField!: boolean;
        constructor() {
            return { someField: true };
        }
    }
    
    0 讨论(0)
  • 2021-02-20 04:05
    "angularCompilerOptions": {
        //   ...
        "strictPropertyInitialization": false
        //   ...   }
    

    According to angular latest version, it supports strict typing, so you have to disbale it.

    Make modifications and restart server

    0 讨论(0)
提交回复
热议问题