Skip to content

Key remapping breaks tuple support for mapped typesΒ #42122

@insidewhy

Description

@insidewhy

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions