color-js / color.js

Color conversion & manipulation library by the editors of the CSS Color specifications
https://colorjs.io
MIT License
1.89k stars 82 forks source link

Add type for keywords #545

Closed lloydk closed 3 months ago

netlify[bot] commented 3 months ago

Deploy Preview for colorjs failed. Why did it fail? →

Name Link
Latest commit b52256659211f19c1d6f7040b35480a0e5f58b9b
Latest deploy log https://app.netlify.com/sites/colorjs/deploys/666784a4b2f51000088cea57
MysteryBlokHed commented 3 months ago

LGTM—I tried messing with @satisfies locally to see if there was a way to keep the color names as part of the type, but it doesn't seem to work

Another option could be to do something like storing the values in a (non-exported) variable, and then doing this?

const colors = { /* ... */ };

/** @typedef {keyof typeof colors} NamedColor */

export default /** @type {Record<NamedColor, [number, number, number]>} */ (colors);
               // ^ JSDoc type assertion

Depends on whether it seems important to preserve the color names in the type. Thoughts?

lloydk commented 3 months ago

LGTM—I tried messing with @satisfies locally to see if there was a way to keep the color names as part of the type, but it doesn't seem to work

Another option could be to do something like storing the values in a (non-exported) variable, and then doing this?

const colors = { /* ... */ };

/** @typedef {keyof typeof colors} NamedColor */

export default /** @type {Record<NamedColor, [number, number, number]>} */ (colors);
               // ^ JSDoc type assertion

Depends on whether it seems important to preserve the color names in the type. Thoughts?

You wouldn't gain any additional type safety because anywhere you'd use the NamedColor type you'd also accept any string (e.g. "oklch(40% .4 180)") but I'm not opposed to it.

LeaVerou commented 3 months ago

IMHO I think it's fine if typing isn't so thorough as to only allow specific strings — and would likely fail in edge cases if we did that.

MysteryBlokHed commented 3 months ago

Alright, this seems good then!