I created this simple component that has two inputs that both accept a string and in theory should accept undefined because they have a default value.
@Component({
selector: 'app-child',
imports: [],
template: `
<p>testString: {{ testString() }}</p>
<p>testStringTransform: {{ testStringTransform() }}</p>
`
})
export class Child {
testString = input<string>("defaultString")
testStringTransform = input<string, string | undefined>("defaultStringTransform", { transform: (v) => v ?? "defaultStringTransform" })
}
In the parent component I use it like shown in the following example:
<app-child [testString]="'inputString'" [testStringTransform]="undefined" />
This gives me the following output.
<app-child>
<p>testString: inputString</p>
<p>testStringTransform: defaultStringTransform</p>
</app-child>
It uses the default value for testStringTransform and the passed input value for testString. I would expect testString to work exactly like testStringTransform if I pass "undefined" like this:
<app-child [testString]="undefined" [testStringTransform]="undefined" />
Actually I am getting a type error:
This exmaple might look silly but it makes more sense if you have optional chaining. For example:
<app-child [testString]="testObject?.stringValue" [testStringTransform]="undefined" />
I'm aware that I could do non-null assertions, add type checks, add a computed for every input that returns a default if the value is undefined or use the transform I used in the example. But this is a lot of boilerplate code. Isn't the initialValue of the input signal exactly for this case?
