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;
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.
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.
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.
In this tute, we will discuss angular property has no initializer and is not definitely assigned in the constructor.
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
Props need to have an exclamation mark appended, i.e. foo!: FormGroup.
Related Issue
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With