-
Notifications
You must be signed in to change notification settings - Fork 12.8k
π¨ Array .map() type too wide #60542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
TS doesn't have direct support for correlated unions as described in #30581. The only support is for generics of the form described at #47109, which is kind of yucky for a case like this: type PairFor = { [T in typeof PAIRS[number] as T[0]]: T[1] }
type MapRet<K extends keyof PairFor> =
{ [P in K]: `${P}-${PairFor[P]}` }[K]
const CONCATENATED =
PAIRS.map(<K extends keyof PairFor>(
value: readonly [K, PairFor[K]]
): MapRet<K> => `${value[0]}-${value[1]}` as const);
type Concatenated = (typeof CONCATENATED)[number];
// type Concatenated = "a-0" | "b-1" | "c-2" I don't know if this is considered a duplicate of #30581 or a design limitation. (edit: π¨β πππ) |
Thank you so much for your detailed help, @jcalz! π I initially tried to get the result Thank you again! |
This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Uh oh!
There was an error while loading. Please reload this page.
π Search Terms
TypeScript Playground Link
π Version & Regression Information
Unknown
β― Playground Link
https://www.typescriptlang.org/play/?#code/MYewdgzgLgBACgQQJICUDKMC8MDaAoGQ3AcgENiAaGABgF0KCidiAjSmARnscOeHYBM3WjFIQYoSFADcePJOgwAwgHkAckoQAVAKJrtOgCJZ4ydADoAtqQAOACgBupADYBXAKZYAfDAAGAEgBvJzd3HDoAXwBaIJCPHC4I31FxBSgASlk8KABPG08lcGBSKHcwEvcAExM7XPyQADNldU1dfV1DdJwwV0sWdwAnWlkgA
π» Code
π Actual behavior
If you hover over
Concatenated
, you can see that its type istype Concatenated = "a-0" | "a-2" | "a-1" | "b-0" | "b-2" | "b-1" | "c-0" | "c-2" | "c-1"
.π Expected behavior
The expected type should instead be
type Concatenated = "a-0" | "b-1" | "c-2"
.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: