xstyled icon indicating copy to clipboard operation
xstyled copied to clipboard

Fix IntelliSense override in Space type by refining Pixel

Open Gavriel-M opened this issue 5 months ago β€’ 9 comments

πŸ› οΈ Fix: Improve Type IntelliSense by Refining Pixel Definition

Issue:

The current Pixel type is defined as string | number, which causes IntelliSense to prioritize raw values over theme tokens (e.g., 'sm', 'md', etc.) when used in union types like Space<T> = ThemeSpace<T> | Pixel.

Solution:

Redefine Pixel as (string & {}) | (number & {}), which maintains compatibility with raw values at runtime but defers IntelliSense suggestions in favor of theme tokens.

// Before
export type Pixel = string | number;

// After
export type Pixel = (string & {}) | (number & {});

This enables cleaner developer experience with proper token autocompletion while retaining full runtime flexibility.

Gavriel-M avatar Jul 14 '25 13:07 Gavriel-M

@quantizor Do you think that's something we can merge? I'm working with custom space tokens (ie. 'space0', 'space2', 'space4' and so on) and I cannot get intellisense for them.

Gavriel-M avatar Jul 14 '25 14:07 Gavriel-M

I don't have sufficient knowledge of TypeScript for this, and I'm not currently in the business of merging xstyled PR's anyway, but I have one request: This sort of non-obvious code should always include internal documentation to explain it and preferably point to reference documentation.

agriffis avatar Jul 14 '25 14:07 agriffis

@agriffis Thanks for the note! This behavior is due to a well-known TypeScript issue where string | number overrides IntelliSense for more specific union types like theme tokens.

I’ll add a comment above the type definition explaining the reasoning and linking to that issue.

Curious β€” do you have a preferred way of documenting this kind of subtle or non-obvious type behavior? Always happy to align with your expectations.

Gavriel-M avatar Jul 14 '25 14:07 Gavriel-M

Internal documentation seems to be a lost art in the modern era...

Something like this:

// This syntax enables Intellisense to prioritize theme tokens over raw values.
// See https://github.com/microsoft/TypeScript/issues/29729

agriffis avatar Jul 14 '25 16:07 agriffis

Haha, it really is. I keep hearing β€œyour code should document itself!” β€” but sometimes it needs a little help.

Just pushed a clear inline comment to document the intention. Thanks again for the suggestion!

Gavriel-M avatar Jul 15 '25 06:07 Gavriel-M

@agriffis For better reference, here’s the IntelliSense improvement in action:

xStyledTSFix

It's a super minimal change, but it makes a big impact on developer experience. Merging this would help a lot β€” right now I’m having to override the type just to restore proper suggestions.

Gavriel-M avatar Jul 15 '25 08:07 Gavriel-M

@quantizor I contributed a review and some comments here, but I won't step on your toes by merging it directly. Assume you'll do that when you're ready.

agriffis avatar Jul 15 '25 17:07 agriffis

πŸ‘‹πŸ» @gregberge WDYT?

galangel avatar Jul 31 '25 06:07 galangel

πŸ‘‹πŸ» @gregberge WDYT?

Hey, I am no longer maintaining xstyled. If you use it you should migrate to Tailwind CSS 😌

gregberge avatar Jul 31 '25 06:07 gregberge