Skip to content

Commit

Permalink
Add guideline for required angular component input (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
MGibson1 authored Dec 16, 2024
1 parent 108200c commit fbb19a3
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/architecture/adr/0014-typescript-strict.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,24 @@ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Null
const foo = null ?? "default string";
```

#### Use `!` on required `@Input` parameters

Typescript's property initialization has no knowledge of Angular's guarantees. That means that

```ts
@Input({ required: true }) options: WebAuthnOptions;
```

will error as `options` is not initialized in the constructor. The suggested fix is to use the
non-null assert operator, `!`.

```ts
@Input({ required: true }) options!: WebAuthnOptions;
```

Once [required input signals][requiredInputs] are out of dev preview for us (Angular 19), we can
transition to using them and initialize these kinds of parameters in the class body or constructor.

[null]: https://www.typescriptlang.org/tsconfig#strictNullChecks
[plugin]: https://github.com/allegro/typescript-strict-plugin
[requiredInputs]: https://angular.dev/guide/components/inputs#required-inputs

0 comments on commit fbb19a3

Please sign in to comment.