Skip to content

TypeScript error while using stylex.create after 0.13.0 update - the inferred type cannot be named without a reference #1070

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

Open
pawelblaszczyk5 opened this issue May 20, 2025 · 7 comments
Labels
bug Something isn't working

Comments

@pawelblaszczyk5
Copy link

Describe the issue

Hello 👋
It seems to be somewhat similar to #857 - in 0.13.0 entry point with StyleXTypes were removed. When using stylex.create I'm getting type errors because TypeScript can't generated type without having access to e.g. StyleXClassNameFor (in my tests re-exporting it from package fixed this particular error but I'm unsure whether there're some more cases where it'd fail.

If I use any stylex.create and TS is configured to generate types for given package it errors:

Image

Image

Expected behavior

Project can be properly typechecked via tsc when declaration generation is enabled and stylex.create results are exported

Steps to reproduce

  1. Install StyleX
  2. Enable declaration generating in your tsconfig by setting declaration to true
  3. Try to create any styles with stylex and export them
  4. Observe error in IDE/tsc failing

I've created small reproduction: https://github.com/pawelblaszczyk5/stylex-incorrect-types-repro

Test case

export const example = stylex.create({
  test: {
    textAlign: "center",
  },
});

and TypeScript with declaration option enabled

Additional comments

As mentioned - it seems like the whole entry point with types got removed from package.json - it seems like some of these types are needed for TS so it can properly build project. In my case I'm using monorepo and having shared styles in a package which is built and I need d.ts files for it

@pawelblaszczyk5 pawelblaszczyk5 added the bug Something isn't working label May 20, 2025
@pawelblaszczyk5 pawelblaszczyk5 changed the title TypeScript error while using stylex.create after 0.13.0 update - the inferred type cannot be named without a reference TypeScript error while using stylex.create after 0.13.0 update - the inferred type cannot be named without a reference May 20, 2025
@necolas
Copy link
Contributor

necolas commented May 20, 2025

The types are all in the package and the export map points to them. Have a look through the published code and let me know if you can tell why you're getting that error now - https://www.npmjs.com/package/@stylexjs/stylex/v/0.13.0?activeTab=code

@pawelblaszczyk5
Copy link
Author

The types are all in the package and the export map points to them. Have a look through the published code and let me know if you can tell why you're getting that error now - https://www.npmjs.com/package/@stylexjs/stylex/v/0.13.0?activeTab=code

These types which are exported here under “types” key doesn’t include all types necessary to generate declaration file. At the screen in the issue you can see that hovered type for the style includes “StyleXClassNameFor” but this type isn’t exported anywhere in public API - hence TS can’t generate the declaration file for it. I hope this explanation is clearer? 😃

What I was referring to was that this entry point got removed from 0.13.0:

Image

@necolas
Copy link
Contributor

necolas commented May 20, 2025

lib/StyleXTypes is not part of the public API. I still don't understand why TypeScript is having problems using the exported types

@pawelblaszczyk5
Copy link
Author

pawelblaszczyk5 commented May 20, 2025

Currently stylex.create in its return type uses types that aren’t exported anywhere e.g. mentioned above StyleXClassNameFor - which is supposed to be opaque type if I understand comments correctly.

From the intellisense screenshot you can see that TS infers type of this variable as some nested Readonly object which has property with value of StyleXClassNameFor<“textAlign”, “center”>. So TS would like to emit something like:

declare const example: Readonly<{
  readonly test: Readonly<{
    textAlign: XXX;
  }>
}>

Where XXX would be something like

import(“@stylexjs/stylex/some-entrypoint-or-no-specific-entrypoint”). StyleXClassNameFor<“textAlign”, “center”>

But it’s impossible because “StyleXClassNameFor” isn’t exported anywhere. It’s the same if user wanted to annotate it manually exactly right - it’s impossible (without stuff like ReturnType) because the type returned by the function isn’t exported anywhere.

@nmn
Copy link
Collaborator

nmn commented May 22, 2025

Typescript made a change somewhat recently where types that don't ever need to be typed manually, such as StyleXClassNameFor, which is an implementation detail may be required to be imported in files so Typescript can generate a friendly string representation of the type.

Interestingly, the types don't actually have to be imported, they only need to be exported from the package, so typescript can represent them in terms of imports.

I haven't quite wrapped my head around the exact requirements, but that seems to be the general idea.

@pawelblaszczyk5
Copy link
Author

I'm pretty sure TypeScript actually needs to import them to generate d.ts files - here's an example of usage of stylex.types(don't mind alias to "@brain/stylex" 😄 ):

Image

And here's an example of stylex.create with StyleX 0.12.0:

Image

Or 0.13.0 with a patch re-exporting the one problematic type that I found:

Image

@deepjoy
Copy link

deepjoy commented May 28, 2025

Still seeing this with 0.13.1

The inferred type of 'styles' cannot be named without a reference to '.../node_modules/@stylexjs/stylex/lib/cjs/types/StyleXTypes'.

looks like this commit only fixed this issue partially

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants