Avoid Unexpected Errors in Angular 16 with Required Attributes

A common issue when working with Angular components is forgetting to pass all of the inputs that the component needs.

Angular 16 introduced some checks for us to help us with this issue.

I can go into any of my component and say declare required inputs.

export class PersonComponent { @Input({ required: true }) person: string = 'Default Person'; }

Now when this component is used anywhere, angular will error out if the input is not provided.

Share with a coworker

Transcript

[00:00] Here, we have an Angular component that takes an input and then it's going to render that input on the template. Now, when we try to render that component, it's going to render its content on the screen.

[00:16] A common issue when working with Angular components is forgetting to pass all of the inputs that the component needs. In our case, we can quickly fix this by passing person and let's say Jorge, and now we have it updated.

[00:35] Angular 16 introduced some checks for us to help us with this issue. I can go into my component and say that this is going to be a required input.

[00:49] For that, I pass the keyboard required as true in the component property. Now when I save this, you see that I'm going to get an error and the application won't be able to build.

[01:06] It says required input person from component person component must be specified. Now there is no chance I forget to add this component. I can just come back here, add the component, and the app will run as expected.