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

Key remapping breaks tuple support for mapped types #42122

Closed
insidewhy opened this issue Dec 27, 2020 · 2 comments
Closed

Key remapping breaks tuple support for mapped types #42122

insidewhy opened this issue Dec 27, 2020 · 2 comments

Comments

@insidewhy
Copy link

insidewhy commented Dec 27, 2020

Bug Report

πŸ”Ž Search Terms

key remapping tuples mapped types

πŸ•— Version & Regression Information

Typescript 4.1

  • I was unable to test this on prior versions because Typescript 4.1 introduced key remapping

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type NoBoolean<K, T> = T extends boolean ? never : K

type FilterBooleans<T> = {
  [K in keyof T as NoBoolean<K, T[K]>]: T[K]
}

type Filtered = FilterBooleans<[string, boolean, number, boolean]>

// should only accept ["1", 3]
const f: Filtered = ["1", true, 3, false]
type Length = Filtered["length"];
// should only accept 2, not 4
const l: Length = 4;

πŸ™ Actual behavior

#26063 introduced mapped type support for arrays and tuples. This breaks when using the new "key remapping" feature added to TypeScript 4.1. A record type is produced that looks like a tuple, rather than the tuple type that would be produced when not using as to remap keys.

πŸ™‚ Expected behavior

A filtered tuple type should be produced.

@insidewhy insidewhy changed the title Remapping keys breaks mapped support for tuples Key remapping breaks tuple support for mapped type Dec 27, 2020
@insidewhy insidewhy changed the title Key remapping breaks tuple support for mapped type Key remapping breaks tuple support for mapped types Dec 27, 2020
@jcalz
Copy link
Contributor

jcalz commented Dec 27, 2020

Duplicate of #40586

I have the same issue here as there: even if you could manage to preserve a tuple type upon key renaming, I don’t see how you’d get a shortened tuple from it. It would be a sparse tuple, if anything. In your mapping, the key "2" would stay "2", right?

@insidewhy
Copy link
Author

@jcalz Thanks for the info. Getting a sparse tuple wouldn't be good for many use cases I think, not unless there was a way to compact it. Filtering an array is possible but not without lots of CPU and a size limit 😭

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