I am currently working on setting up @Input for my component using a model that resembles the following:
interface Car {
sail?: never
tires: number
weight: number
}
interface Boat {
tires?: never
sail: boolean
weight: number
}
export type Vehicle = Car | Boat;
The objective is to allow inputs like this (which works without any errors):
@Input() Car: Vehicle = {weight: 2500, tires: 4}
@Input() Boat: Vehicle = {weight: 2500, sail: true}
This results in an error as expected:
@Input() Mixed: Vehicle = {weight: 2500, tires: 4, sail: true}
In my HTML file, I have the following setup (where only 'tires' or 'sail' should be accepted, not both - which should trigger an error):
<component [car]="{weight: 2500, tires: 4, sail: true}"></component>
Surprisingly, Angular does not throw an error when receiving input from the HTML file. However, what perplexes me is how the Vehicle
type can be filled with incorrect data. What would be the best approach to address this issue?