Home > Enterprise >  Are @Input() readonly good practice in Angular?
Are @Input() readonly good practice in Angular?

Time:06-29

Is it considered a good practice to use the keyword readonly on @Input() fields in Angular ?

Or are there any drawbacks ?

CodePudding user response:

You won't be able to use the @Input() field.

Error: src/app/app.component.html:6:12 - error TS2540: Cannot assign to 'foo' because it is a read-only property.

6 <app-test [foo]="5"></app-test>
         ~~~

You can get around this error by removing

"strictInputAccessModifiers": true

EDIT: So in conclusion, I would advise against it since strictInputAccessModifiers does more than just throwing this particular error for this particular use case of readonly. Although I do think there's some merit to blocking the manual assignment of @Input() fields.

  • Related