stitches: Compiling Stitches without "strict": true, causes massive performance issues
Bug report
Describe the bug
When compiling Stitches in a TS environment without "strict": true in tsconfig.json the compile time will jump from a few seconds to multiple minutes (~200 seconds).
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
1 Clone Repo
git clone git@github.com:andrewgeorgemitchell/stitches-strict-repro.git
2 Install deps
yarn install
3 Run TS compiler (will take ~160-200 seconds)
yarn tsc
4 Enable strict mode in tsconfig.json line 10
"strict": true,
5 Run TS compiler (will take ~4 seconds)
yarn tsc
Expected behavior
TS build should not take ~200 seconds
Screenshots
Screen shot of slow compile (169.41s): ('strict': false)

Screen shot of normal compile (4.00s): ('strict': true)

System information
- OS: [macOS]
- Browser (if applies) [N/A]
- Version of Stitches: [1.2.8]
- Version of Node.js: [16.x]
- Version of Typescript: [4.6.x, 4.7.x] (tested both)
Additional context
Possible Cause & Interesting observation
The issue when not in strict mode seems to be that Typescript spends a large amount of time comparing CSS objects to each other, you can see this behaviour for yourself by:
- commenting out line 71 in
src/components/Text/Text.tsx - uncommenting line 72 in
src/components/Text/Text.tsx
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 4
- Comments: 15 (4 by maintainers)
Commits related to this issue
- finally fix slow typescript compilation! see https://github.com/stitchesjs/stitches/issues/1038 — committed to jakejarvis/jarv.is by jakejarvis 2 years ago
- Replace union member CSS with any to speed up type checking massively. See https://github.com/stitchesjs/stitches/issues/1038#issuecomment-1516638929 — committed to DrJKL/stitches by deleted user 8 months ago
Similar to others here, I had major issues getting this to work, whether with or without
strictmode. After doing a lot of performance tracing on type checking, it appears to come down to theCSStype incss-utils.d.ts. I made a couple modifications to stitches in order to support updating TypeScript, including patching for the issue in #1078.The key change though for the performance problem, not requiring any change to my code, was to modify the
unknown propertytype intersection incss-util.d.ts. This is the diff:This completely resolved my performance problem, and I don’t think it will have significant negative impact on the developer experience. (NOTE: I’m using patch-package to apply this patch in my repo as we aren’t on yarn v2 yet)
Typescript is the gift that keeps on giving 😄 I remember fixing a similar issue about a year ago. Thanks for reporting it.
@hadihallak Glad I could help/finally figured this one out 👍 We are loving Stitches as we use it to build out our design-system @ Drift so just let me know if there is anything I can do to help.
One more thing that just popped into my head that may or may not be relevant is that in the reproduction repo I only have 1 component which uses the line:
css={css}While in the design-system this reproduction repo is based on we have a lot more components and yet when looking at the tsc --generateTrace trace its pretty obvious that it only slows down the first time it runs into
css={css}and when it runs subsequent times it finishes them in seconds, so yeah just further evidence I guess that something strange is happening with typescript and comparing those types.
Node v16.15.1 TypeScript v4.7.3
{ "strict": true }: (1.63s){ "strict": false }: (153.53s)FWIW, the project is here: https://github.com/jakejarvis/jarv.is
For anyone who’s reading this and is experiencing degraded types performance, Please comment the time it takes to build your project with and without setting
"strict": truein tsconfig.json Also please include both the TS and Node versionsOh my goodness, THANK YOU @andrewgeorgemitchell for this quick fix! 🙏
If it helps at all @hadihallak, my Stitches project was fine with TS 4.5, got very slow in 4.6, was working great again in 4.7.2 and slow again in 4.7.3 (before changing
strictto true). Could just be my individual experience but maybe a clue lies somewhere in the latest changelog…? (To be fair this back-and-forth flakiness could also mean the problem is more on TypeScript’s side than here!)https://github.com/microsoft/TypeScript/issues?q=milestone%3A"TypeScript+4.7.3"+
https://github.com/microsoft/TypeScript/compare/v4.7.2...v4.7.3
@andrewgeorgemitchell btw, really appreciate this detailed issue. super helpful 🙏