Sub topic 4
Making an input required
This chapter assumes you know TypeScript generics, Angular standalone components, property binding, and how to read a signal by calling it. You have already seen input() for optional inputs. Here you will see how input.required() makes a value mandatory and what that changes.
Why require an input
Some components make no sense without data. A user card with no user has nothing to show. With a plain input(), you must supply a default value, and a made-up default such as an empty string can hide a missing binding.
input.required() removes the default. The parent must bind the input, and Angular checks this at compile time.
Declaring a required input
import { Component, input } from '@angular/core';
@Component({
selector: 'app-user-card',
template: `
<h2>{{ name() }}</h2>
<p>Role: {{ role() }}</p>
`,
})
export class UserCard {
name = input.required<string>();
role = input('guest');
}name is required and has type string. role is optional with the default 'guest', so its type is inferred as string. Both are read by calling them like functions.
Note the type argument. A required input has no initial value to infer from, so you write the type yourself with input.required<string>().
Binding it in the parent
<app-user-card [name]="user.name" role="admin" />The parent binds name with property binding and sets role with a static attribute value. If you remove the name binding, the Angular compiler reports an error for the template. The build fails instead of the page rendering a blank heading.
Optional versus required
| Declaration | Default value | Type of the signal | Parent must bind it | Column |
|---|---|---|---|---|
| `input('guest')` | `'guest'` | `InputSignal<string>` | No | |
| `input<string>()` | `undefined` | `InputSignal<string \ | undefined>` | No |
| `input.required<string>()` | None | `InputSignal<string>` | Yes |
The second row matters. An optional input with no default has undefined in its type, so every read needs a check. A required input gives you a clean string. When a component truly needs a value, prefer input.required() over input<string>().
Reading a required input safely
Reading a required input before the parent has set it throws an error. Angular sets inputs after it creates the component, so the constructor and field initializers run too early.
This code fails:
import { Component, input } from '@angular/core';
@Component({
selector: 'app-broken-card',
template: `<p>{{ name() }}</p>`,
})
export class BrokenCard {
name = input.required<string>();
upper = this.name().toUpperCase(); // throws: the input is not set yet
}The field initializer calls this.name() while the component is being built. The fix is to defer the read. A computed() runs only when something reads it, which happens after the inputs are set:
import { Component, computed, input } from '@angular/core';
@Component({
selector: 'app-card',
template: `<p>{{ upper() }}</p>`,
})
export class Card {
name = input.required<string>();
upper = computed(() => this.name().toUpperCase());
}The next chapter covers computed() in more detail. Lifecycle hooks such as ngOnInit are also safe places to read inputs.
Common errors
- Forgetting the type argument.
input.required()without<T>gives youunknown. - Reading in the constructor. Move the read into
computed(),effect(), orngOnInit. - Passing
undefinedon purpose. If a value can legitimately be missing, the input is not required. Useinput<string>()and handleundefined. - Writing to the input. Inputs are read-only signals. A child cannot call
set()on them.
Exercise
A component declares title = input.required<string>(); and a field shout = this.title().toUpperCase();. The app throws an error at run time. Why does it happen, and how do you fix it?
Show answer
The field initializer runs while Angular constructs the component, before the parent's value has been set, so reading the required input throws. Replace the field with shout = computed(() => this.title().toUpperCase());, which reads the input only when shout() is called after the inputs are set.
Exercise
You write <app-user-card role="admin" /> for a component whose name is input.required<string>(). What happens, and when?
Show answer
The Angular compiler reports an error at build time because the required input name is not bound. The page never renders without it.