Previously, we have problem for such code:
type RGB = readonly [red: number, green: number, blue: number];
type Color = { value: RGB | string };
const myColor: Color = { value :'red' };
myColor.value.toUpperCase() // Property 'toUpperCase' does not exist on type 'string | RGB'.
With TS 4.9, it works
type RGB = readonly [red: number, green: number, blue: number];
type Color = { value: RGB | string };
const myColor = { value :'red' } satisfies Color;
myColor.value.toUpperCase() // works, now myColor.value is narrowing down to string type
Working with as const
type RGB = readonly [red: number, green: number, blue: number];
type Color = RGB | string;
const palette = {
red: [255, 0, 0],
green: "#00ff00",
blue: [1,2,3],
} satisfies Record<string, Color>;
console.log(palette.green); // green is a string type
const constantPalette = {
red: [255, 0, 0],
green: "#00ff00",
blue: [1,2,3],
} as const satisfies Record<string, Color>;
console.log(constantPalette.green); // green is "#00ff00"
标签:TypeScript,4.9,number,value,RGB,green,operator,type,red From: https://www.cnblogs.com/Answer1215/p/17060995.html