You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We create "substitution types" in conditional types - when writing T extends U ? TrueBranch : FalseBranch, we substitute T with something that's sort of equivalent to T & U in the TrueBranch.
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" and keyof T so that you can index into T with it in the true branch!
Example:
// Ensure the following real-world example that relies on substitution still workstypeExtractParameters<T>="parameters"extendskeyofT// The above allows "parameters" to index `T` since all later// instances are actually implicitly `"parameters" & keyof T`
? {
[KinkeyofT["parameters"]]: T["parameters"][K];}[keyofT["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?
Activity
awerlogus commentedon Apr 8, 2021
Daniel Rosenwasser (@DanielRosenwasser)
It's possible to write log2(n) algorithm instead of the linear one after #43591 is fixed