Closed
Description
Tracking Substitution Type Replacement
- We create "substitution types" in conditional types - when writing
T extends U ? TrueBranch : FalseBranch
, we substituteT
with something that's sort of equivalent toT & U
in theTrueBranch
. - Doing a "dumb" substitution.
- When did we start supporting substitution types on primitives/literals?
-
Turns out it's relied upon quite a bit!
-
If you write
"foo" extends keyof T ? ... : ...
, you want to make a substitution type of"foo"
andkeyof T
so that you can index intoT
with it in the true branch! -
Example:
// Ensure the following real-world example that relies on substitution still works type ExtractParameters<T> = "parameters" extends keyof T // The above allows "parameters" to index `T` since all later // instances are actually implicitly `"parameters" & keyof T` ? { [K in keyof T["parameters"]]: T["parameters"][K]; }[keyof T["parameters"]] : {};
-
- You can figure that in contravariant positions, you don't actually want to produce the substitution type.
- The issue is that not every use of a type implies strict variance. You could end up with some invariant and covariant and contravariant uses.
- Should you create unions in contravariant positions?
- Hard to reason about - leave it alone for now.
- PR seems right, get some more eyes on it.
Fixing Excessive Stack Depth Issues
- Lot of people hitting this issue.
- Tracks back to Add missing relationship allowing a type to be assignable to a conditional when assignable to both branches #30639
- Came up with a targeted fix that special-cases arrays, but doesn't fix everything.
- Unclear if there's a potential better way to write
FlatArray
. Will experiment.
Performance Depends on Declaration Order
- If you have a
const
that uses an interface before it's declared...it's somehow slower! - The spread is also suspicious - spreads can create huge anonymous types.
- Potentially related to lower type IDs.
- Subtype reduction picks the one with a lower type ID, but that could be worse to compare against.
- Also, might just not be hitting the relationship cache.
- Investigation: start at high number, go down.
- It didn't work. 😱
- Unclear what to do about it though.