Closed
Description
interface RGB {
red: number;
green: number;
blue: number;
}
function setColor(value: unknown) {
const isRGB =
// is an object
value && typeof value === "object" &&
// 'red' exists and is a 'number'
"red" in value && typeof value.red === "number" &&
// 'green' exists and is a 'number'
"green" in value && typeof value.green === "number" &&
// 'blue' exists and is a 'number'
"blue" in value && typeof value.blue === "number";
if (isRGB) {
const rgb: RGB = value;
}
}
Expected: No errors
Actual Errors.
Type 'object & Record<"red", unknown> & Record<"green", unknown> & Record<"blue", unknown>' is not assignable to type 'RGB'.
Types of property 'red' are incompatible.
Type 'unknown' is not assignable to type 'number'.