stylex icon indicating copy to clipboard operation
stylex copied to clipboard

fix: Typescript type fixes for themes and types functions

Open nmn opened this issue 8 months ago • 6 comments

What changed / motivation ?

Made some changes to the Flow types so that the generated Typescript types are valid.

Also made some changes to a typescript .d.ts file to make it valid.

Linked PR/Issues

Fixes #969

Additional Context

Some type constraints loosened up so various stylex.types.* classes would have the same constraints to make things work correctly in Typescript.

This doesn't actually make the types less strict because the type constraints on the actual functions are unchanged.

nmn avatar Apr 28 '25 05:04 nmn

workflow: benchmarks/perf

Comparison of performance test results, measured in operations per second. Larger is better.

[email protected] compare node ./compare.js /tmp/tmp.JjlVgwCDX0 /tmp/tmp.jh29Uk4po7

Results Base Patch Ratio
babel-plugin: stylex.create
· basic create 519 516 0.99 -
· complex create 186 182 0.98 -
babel-plugin: stylex.createTheme
· basic themes 445 448 1.01 +
· complex themes 41 41 1.00

github-actions[bot] avatar Apr 28 '25 05:04 github-actions[bot]

workflow: benchmarks/size

Comparison of minified (terser) and compressed (brotli) size results, measured in bytes. Smaller is better.

[email protected] compare node ./compare.js /tmp/tmp.UlZUofh8C7 /tmp/tmp.KnPudfhZjf

Results Base Patch Ratio
@stylexjs/stylex/lib/cjs/stylex.js
· compressed 1,200 1,200 1.00
· minified 3,513 3,513 1.00
@stylexjs/stylex/lib/cjs/inject.js
· compressed 1,227 1,227 1.00
· minified 3,224 3,224 1.00
benchmarks/size/.build/bundle.js
· compressed 537,611 537,611 1.00
· minified 7,435,904 7,435,904 1.00
benchmarks/size/.build/stylex.css
· compressed 100,509 100,509 1.00
· minified 754,513 754,513 1.00

github-actions[bot] avatar Apr 28 '25 05:04 github-actions[bot]

Looks good to me.

AlexanderOMara avatar Apr 28 '25 13:04 AlexanderOMara

Oh, one other new issue compared to the current release. Looks like StyleX$DefineConsts is missing in StyleXTypes.d.ts (out-of-sync since it was added to StyleXTypes.js), it has the same type as StyleX$DefineVars and easy to add:

export type StyleX$DefineVars = <
  DefaultTokens extends TTokens,
  ID extends symbol = symbol,
>(
  tokens: DefaultTokens,
) => VarGroup<FlattenTokens<DefaultTokens>, ID>;

export type StyleX$DefineConsts = <
  DefaultTokens extends TTokens,
  ID extends symbol = symbol,
>(
  tokens: DefaultTokens,
) => VarGroup<FlattenTokens<DefaultTokens>, ID>;

With StyleX$DefineConsts added this branch produces types that are strictly valid!

AlexanderOMara avatar Apr 28 '25 14:04 AlexanderOMara

StyleXTypes.d.ts (out-of-sync since it was added to StyleXTypes.js),

Can we avoid having a manual StyleXTypes.d.ts file and generate that too?

necolas avatar Apr 28 '25 18:04 necolas

That would probably be better to avoid inconsistencies, looks like there might be some other inconsistencies.

Seeing these errors when StyleXTypes.d.ts is generated with the code in this branch:

$ tsc

.../stylex/lib/StyleXTypes.d.ts:89:24 - error TS2411: Property 'default' of type 'A' is not assignable to 'string' index type 'B'.

89               readonly default: infer A;
                          ~~~~~~~

.../stylex/lib/StyleXTypes.d.ts:106:51 - error TS2344: Type 'Obj' does not satisfy the constraint '{ readonly [$$Key$$: string]: unknown; }'.

106       ? (...args: Args) => Readonly<[MapNamespace<Obj>, InlineStyles]>
                                                      ~~~

  .../stylex/lib/StyleXTypes.d.ts:105:69
    105     [Key in keyof S]: S[Key] extends (...args: infer Args) => infer Obj
                                                                            ~~~
    This type parameter might need an `extends { readonly [$$Key$$: string]: unknown; }` constraint.

.../stylex/lib/StyleXTypes.d.ts:107:22 - error TS2344: Type 'S[Key]' does not satisfy the constraint '{ readonly [$$Key$$: string]: unknown; }'.
  Type 'S[keyof S]' is not assignable to type '{ readonly [$$Key$$: string]: unknown; }'.
    Type 'S[string] | S[number] | S[symbol]' is not assignable to type '{ readonly [$$Key$$: string]: unknown; }'.
      Type 'S[string]' is not assignable to type '{ readonly [$$Key$$: string]: unknown; }'.

107       : MapNamespace<S[Key]>;
                         ~~~~~~

.../stylex/lib/StyleXTypes.d.ts:113:3 - error TS2411: Property '$$css' of type 'true' is not assignable to 'string' index type 'string'.

113   $$css: true;
      ~~~~~

.../stylex/lib/StyleXTypes.d.ts:116:39 - error TS2411: Property '$$css' of type 'void | undefined' is not assignable to 'string' index type 'string'.

116 export type InlineStyles = Readonly<{ $$css?: void; [key: string]: string }>;
                                          ~~~~~

.../stylex/lib/StyleXTypes.d.ts:177:20 - error TS2411: Property 'default' of type 'X' is not assignable to 'string' index type 'Y'.

177           readonly default: infer X;
                       ~~~~~~~


Found 6 errors in the same file, starting at: .../stylex/lib/StyleXTypes.d.ts:89

AlexanderOMara avatar Apr 28 '25 18:04 AlexanderOMara