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

[types] Merge Color types #259

Merged
merged 3 commits into from
Jan 27, 2023

Conversation

MysteryBlokHed
Copy link
Member

@MysteryBlokHed MysteryBlokHed commented Dec 5, 2022

Closes #258

The types originally had two definitions for the Color type which weren't interchangeable. This PR changes the types to more accurately reflect the original JS by augmenting the existing Color type when src/index.d.ts is imported.

There is a problem with how I've done this right now: the module augmentation is happening even when the normal src/color.d.ts version of the class is imported. I'm not sure how this is happening without src/index.d.ts even being referenced, so I'll have to debug that when I get the chance. Any help to make sure the augmentation only happens when src/index.d.ts is imported would be appreciated.

You can look at the failing test to see the problem: https://github.com/LeaVerou/color.js/pull/259/files#diff-5bb8dd015c2660f438205df14840aa404517154744bcf054146734f0d5f8ad26

The types originally had two definitions for the `Color` type which
weren't interchangeable. This PR changes the types to more accurately
reflect the original JS by augmenting the existing `Color` type when
`src/index.d.ts` is imported.

There is a problem with how I've done this right now: the module
augmentation is happening even when the normal `src/color.d.ts` version
of the class is imported. I'm not sure how this is happening without
`src/index.d.ts` even being referenced, so I'll have to debug that when
I get the chance.
@LeaVerou
Copy link
Member

LeaVerou commented Dec 5, 2022

I can't help much with the TS side of things, but thank you so much for working on this!

@MysteryBlokHed
Copy link
Member Author

I completely forgot I opened this PR 😅
From my understanding of TypeScript, it’s not possible to only augment types when a certain file is imported, which is what I was trying to do since the src/index.js file modifies the original Color import.

The only solution I can think of is to merge this as-is. The only problem with that is it will cause properties that only exist when src/index.js is imported to always be present in the types, even when they don’t actually exist in the current context.

@MysteryBlokHed MysteryBlokHed marked this pull request as ready for review January 12, 2023 13:28
@LeaVerou
Copy link
Member

I’m about to release a new version, should I merge this in?

@MysteryBlokHed
Copy link
Member Author

I think it's ready to be merged. The only problem I noted is that some properties that only exist after importing src/index.js instead of src/color.js are always present in the types, but I don't see any way around that 🤷

@LeaVerou
Copy link
Member

I think it's ready to be merged. The only problem I noted is that some properties that only exist after importing src/index.js instead of src/color.js are always present in the types, but I don't see any way around that 🤷

Yeah, TS doesn't do that well with very dynamic JS. I guess it's better if they always exist than if they never exist.

@LeaVerou LeaVerou merged commit c12f858 into color-js:main Jan 27, 2023
@MysteryBlokHed MysteryBlokHed deleted the types/merge-color-types branch January 27, 2023 20:55
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.

There's two type definitions of Color instance
4 participants