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

Viewed 16073

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;
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

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.

Related