Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve 'Property 'foo' has no initializer and is not definitely assigned in the constructor' when using @Input decorator?

How would I correctly initialize a property that has an @Input decorator without removing strict typing?

Code below is what showing this warning

@Input
foo: FormGroup;
like image 420
Shane Avatar asked Nov 09 '18 10:11

Shane


People also ask

Why property has no initializer in the constructor?

We may get the message Property has no initializer and is not definitely assigned in the constructor when adding some configuration in the tsconfig.json file so as to have an Angular project compiled in strict mode: Indeed the compiler then complains that a member variable is not defined before being used.

Is the property ‘paginator’ not assigned in the constructor?

Property ‘paginator’ has no initializer and is not definitely assigned in the constructor Guys I got this error during code implementing Material Datatable into my Angular 13 Application and using below code snippet and add that inside our component.ts file and we can solved that issue.

Why does property ‘X’ have no initializer and is not definitely assigned?

Property has no initializer and is not definitely assigned by Stephen Fluin Twitter_Logo_White-on-Blue @StephenFluin2018-06-26 If you have ever seen an error that looks like Property 'X' has no initializer and is not definitely assigned in the constructor., then you probably just updated your TypeScript version or strictness flags.

Does angular property have an initializer?

In this tute, we will discuss angular property has no initializer and is not definitely assigned in the constructor.


3 Answers

There is no way to define a decorator such that Typescript will know it will perform field initialization. The only option is to add a definite assignment modifier to the field:

@Input
foo!: FormGroup;

This will disable the check for this field alone. You can read more about this assertion here

like image 65
Titian Cernicova-Dragomir Avatar answered Oct 16 '22 21:10

Titian Cernicova-Dragomir


Props need to have an exclamation mark appended, i.e. foo!: FormGroup.

Related Issue

like image 2
Sajeetharan Avatar answered Oct 16 '22 20:10

Sajeetharan


You can make the property optional, with "?".

@Input
foo?: FormGroup;

In addition, you can make the input required in the selector component

selector: 'app-name[foo]'

So that the component is selected only if it has foo input bound.

like image 1
Alex Parloti Avatar answered Oct 16 '22 22:10

Alex Parloti