Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

TypeScript show following error message to this code samples:

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

Property 'someField' has no initializer and is not definitely assigned in the constructor.


(property) MyClass.someField: boolean

TypeScript Playground (You need to enable strictNullChecks and strictPropertyInitialization to see this error message.)

Given code snippet is simplified from my original script. I would like to return the constructed value instead of assign values to this in constructor. What should I do to make TypeScript works without mentioned error?

like image 333
tsh Avatar asked Jan 09 '19 06:01

tsh


People also ask

What does no initializer and is not definitely assigned in the constructor Boolean?

The error "Property has no initializer and is not definitely assigned in the constructor" occurs when we declare a class property without initializing it. To solve the error, provide an initial value for the class property, e.g. name: string = 'James'; or use a non-null assertion.

How do I disable strictPropertyInitialization?

You can set the strictPropertyInitialization setting to false in your tsconfig. json 's compilerOptions , or --strictPropertyInitialization false on the command line to turn off this checking.

What does no initializer and is not definitely assigned in the constructor in angular 12?

Solution 1: Disable strictPropertyInitialization flag The simple way to fix this error in Angular applications is to disable --strictPropertyInitialization flag in typescript compiler options in tsconfig. json file.


1 Answers

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 };     } } 
like image 148
Titian Cernicova-Dragomir Avatar answered Sep 19 '22 17:09

Titian Cernicova-Dragomir