Skip to content

Typescript 4.1+: Nested template type deduction error #41931

Closed
@YunHsiao

Description

@YunHsiao

TypeScript Version: 4.2.0-dev.20201211

Search Terms: nested templates, extract

Code

type Enum = Record<string, string | number>;
type TypeMap<E extends Enum> = { [key in E[keyof E]]: number | boolean | string | number[] };

class BufferPool<E extends Enum, M extends TypeMap<E>> {
    setArray1<K extends keyof M>(_: K, array: Extract<M[K], ArrayLike<any>>) {
        array.length; // correct
    }
    setArray2<K extends E[keyof E]>(_: K, array: Extract<M[K], ArrayLike<any>>) {
        array.length; // compile-time error, which is NOT expected: 'length' does not exist on type 'Extract<M[K], ArrayLike<any>>'
    }
}

Expected behavior:
Code compiles without error.

Actual behavior:
Compiler throws Property 'length' does not exist on type 'Extract<M[K], ArrayLike<any>>'.
Behavior starts at 4.1 I think, the exact same code works prior to that.

Note that if we change the definition on the first line to:

type Enum = Record<string, string | number | any>;

Then it works fine.

Playground Link:
here

Related Issues:
maybe related to #41380, if so this is a relatively simpler test case.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions