TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Mistake in the docs

Open m-Pawlowicz opened this issue 2 years ago • 8 comments

Acknowledgement

  • [X] I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

Comment

Hi guys, correct me if im wrong, but i believe the docs for satisfies keyword have an error

type Colors = "red" | "green" | "blue";
type RGB = [red: number, green: number, blue: number];
const palette: Record<Colors, string | RGB> = {
    red: [255, 0, 0],
    green: "#00ff00",
    bleu: [0, 0, 255]
//  ~~~~ The typo is now correctly detected
};
// But we now have an undesirable error here - 'palette.red' "could" be a string.
const redComponent = palette.red.at(0);

since values are unions of string and array, palette.red.at(0) will not throw an error, because at method is a method that exists for both types. Cheers :).

image

m-Pawlowicz avatar Jan 10 '24 10:01 m-Pawlowicz