Skip to content
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

🚨 Array .map() type too wide #60542

Open
regnaio opened this issue Nov 20, 2024 · 2 comments
Open

🚨 Array .map() type too wide #60542

regnaio opened this issue Nov 20, 2024 · 2 comments

Comments

@regnaio
Copy link

regnaio commented Nov 20, 2024

πŸ”Ž Search Terms

TypeScript Playground Link

πŸ•— Version & Regression Information

Unknown

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/MYewdgzgLgBACgQQJICUDKMC8MDaAoGQ3AcgENiAaGABgF0KCidiAjSmARnscOeHYBM3WjFIQYoSFADcePJOgwAwgHkAckoQAVAKJrtOgCJZ4ydADoAtqQAOACgBupADYBXAKZYAfDAAGAEgBvJzd3HDoAXwBaIJCPHC4I31FxBSgASlk8KABPG08lcGBSKHcwEvcAExM7XPyQADNldU1dfV1DdJwwV0sWdwAnWlkgA

πŸ’» Code

const PAIRS = [
    ['a', 0],
    ['b', 1],
    ['c', 2],
] as const;

const CONCATENATED = PAIRS.map(value => `${value[0]}-${value[1]}` as const);

type Concatenated = (typeof CONCATENATED)[number];

πŸ™ Actual behavior

If you hover over Concatenated, you can see that its type is type 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

@jcalz
Copy link
Contributor

jcalz commented Nov 20, 2024

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"

Playground link

I don't know if this is considered a duplicate of #30581 or a design limitation.

(edit: πŸš¨β“ πŸš‘πŸš”πŸš’)

@regnaio
Copy link
Author

regnaio commented Nov 20, 2024

Thank you so much for your detailed help, @jcalz! πŸ˜„

I initially tried to get the result "a-0" | "b-1" | "c-2" with another Playground, though I can see why this other approach gives the wider result "a-0" | "a-2" | "a-1" | "b-0" | "b-2" | "b-1" | "c-0" | "c-2" | "c-1". Is there a way to get the narrower result along the lines of this approach? Or would the solution you shared above be the simplest way to achieve this?

Thank you again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants