There are two types:
type Car = {
brand: string
model: string;
firstRegistration?: number;
}
type Color = "red" | "blue" | "green";
How can I create new type OtherCar that has brand type from Car together with Color type?
What I have tried:
type OtherCar = {
brand: Pick<Car, 'brand'>;
color: Color;
}
function makeCar({ brand, color }: OtherCar): string {
return `${brand}, ${color}`
}
When calling
makeCar({ brand: 'Opel', color: 'red' });
There is an error - TS2322: Type 'string' is not assignable to type Pick<Car, "brand">.
{ brand: string; }. It seems you just want a string. So use string, not Pick<Car, "brand">brandchanges fromstringto something else, then I would need to update the type it two places. What would be the right way to reference the type from theCar?makeCar({ brand: { brand: 'Opel' }, color: 'red' });This will work causebrandinOtherCaris an object of Car but just withbrandproperty.