Context
I recently upgraded to a newer version of Angular (I moved up, in three steps, from 10 to 13).
One of the upgrade steps included a dependency on a new version of typescript, which "strict" compiler mode raises an error, if a declared variable doesn't get assigned a value at initialization.
As consequence, all my components now fail the TS build process, because properties decorated as @Input()
are usually not initialised.
Now I face a choice: either I turn off the strict mode (thing which I absolutely don't want to do, especially with this new feature that I find extremely useful), or I go and add an exclamative point on every single @Input()
in every single component, like this
@Input() something!: SomeType;
(this is a way to tell the TS compiler to ignore the uninitialised property)
Question
I would like to know what is Angular's official policy on this topic, whether there are any accepted best practices.
My thoughts
The core of the problem is that @Input()
properties on an Angular component aren't meant to be initialised in the same way as regular properties, since their value comes from the parent component in the template. So, I'd expect them to be treated differently in an Angular context.
I would like to find a way to "delegate" the responsibility, from Typescript to Angular, of this specific strict check.
Ideally, I would like to bypass the Typescript strict check, and mark instead each input as compulsory / optional as part of the specific TS build process for Angular: Angular would look at each instance of that component, and, based on the context where the component is called in the parent HTML template, it would decide whether raising a compiler error
For example, if my component contains two inputs like these
@Input() compulsoryProp: string;
@Input() optionalProp: string = 'default value';
the new Typescript compiler would fail on compulsoryProp
, and it would try to force me to turn it into something like
@Input() compulsoryProp: string | undefined = undefined;
This is what I don't want to do. I would like Angular to figure this out on its own, and raise a compiler error ONLY if I am calling the component, in a parent template, without passing the property compulsoryProp
.
Apologies if this question has already been answered elsewhere. I was surprised to find very few discussions about this topic: the principal thing that comes up on Google results is this other question, but the answer is not satisfactory