Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type for keywords #545

Merged
merged 1 commit into from
Jun 11, 2024
Merged

Conversation

lloydk
Copy link
Collaborator

@lloydk lloydk commented Jun 10, 2024

No description provided.

Copy link

netlify bot commented Jun 10, 2024

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

Name Link
🔨 Latest commit b522566
🔍 Latest deploy log https://app.netlify.com/sites/colorjs/deploys/666784a4b2f51000088cea57

Copy link
Member

@MysteryBlokHed MysteryBlokHed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@MysteryBlokHed
Copy link
Member

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
Copy link
Collaborator Author

lloydk commented Jun 11, 2024

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
Copy link
Member

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
Copy link
Member

Alright, this seems good then!

@MysteryBlokHed MysteryBlokHed merged commit e9231b0 into color-js:main Jun 11, 2024
0 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants