microsoft / microsoft/TypeScript
TypeScript should have optimized this conditional check
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
overloads, conditional types, aliases, performance, caching
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about aliases and conditional types.
⏯ Playground Link
https://github.com/solana-labs/solana-web3.js/pull/1760
💻 Code
Context
I had a large conditional type of the form:
type Overloads<T> =
T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
}
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4]
: T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
}
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3]
: T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
}
? [(...args: A1) => R1, (...args: A2) => R2]
: T extends {
(...args: infer A1): infer R1;
}
? [(...args: A1) => R1]
: unknown;
This refactor reduced check times in my project by 100x:
type Overloads<T> = Overloads4<T>;
type Overloads4<T> = T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
}
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4]
: Overloads3<T>;
type Overloads3<T> = T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
}
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3]
: Overloads2<T>;
type Overloads2<T> = T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
}
? [(...args: A1) => R1, (...args: A2) => R2]
: Overloads1<T>;
type Overloads1<T> = T extends {
(...args: infer A1): infer R1;
}
? [(...args: A1) => R1]
: unknown;
Repro
# Check out the repo before the change
git clone git@github.com:solana-labs/solana-web3.js.git
cd solana-web3.js
git checkout ce145c805ee15ce0c5ef97623d9cfd006cf0b00b
pnpm i
cd packages/library/
pnpm turbo compile:js compile:typedefs
pnpm tsc -p ./tsconfig.json --noEmit --generateTrace tracing_output_folder
# Open tracing_output_folder/trace.json in chrome://tracing
rm -rf tracing_output_folder/
# Move to the commit after the change
git checkout ba9b4c72cb0dfc0a36fed1a0641a19492dd94624
pnpm turbo compile:js compile:typedefs
pnpm tsc -p ./tsconfig.json --noEmit --generateTrace tracing_output_folder
# Open tracing_output_folder/trace.json in chrome://tracing
🙁 Actual behavior
Check times in the project were >10s and Intellisense recomputed the entire project on every keystroke (ie. it took >10s to get typehints and autocomplete).
🙂 Expected behavior
TypeScript should have been able to optimize (ie. cache) this check on its own, without the need for the developer to manually alias each conditional type.
Additional information about the issue
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the comparison using the two listed commits and the packages/library TypeScript commands, then inspect the generated tracing_output_folder/trace.json files in chrome://tracing. Investigate conditional-type checking and caching behavior; done means the equivalent unaliased checks avoid redundant work without changing their type results.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers, performance
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100