css: Improve type-checking performance of CSSVarFunction
#1557
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1556.
Simplifying the CSS var function fallback string literal type from
`var(--${string}, ${string | number})`
to
`var(--${string}, ${string})`
drastically improves type-checking in certain cases. For example, when passing a template literal that interpolates a CSS variable into a
style
property multiple times:This doesn't actually affect the values this type can accept because
${number}
is a subset of${string}
, and therefore${string | number}
===${string}
.With this change, a trivial app containing the above
example.css.ts
file has its type check time drop from ~2.8s to ~0.15s (along with much fewer type instantiations and lower memory usage).Before
After
Benchmarks
I've created a
benchmarks
private package to add benchmarking scripts to. Currently the only script is one that benchmarks the number of instantiations between using a CSS var with a narrow type vs a broad (string
) type.Here are the results of the benchmark before and after making the change in this PR:
Before
After
Before the change in this PR, the narrow type resulted in ~360k instantiations while the broad type resulted in ~8k instantiations (44x difference). With this change, the narrow type now only creates ~12k instantiations (1.5x difference).