Skip to content

Conditional type to mapped type to constraint fails #29141

@samhh

Description

@samhh

TypeScript Version: 3.3.0 dev (master), 3.2.2, 3.2.1

Search Terms: "conditional narrow", "conditional mapped", "conditional constraint"

Code:

type MapArray<T extends any[]> = {
    [P in keyof T]: T[P];
};

type Constraint<T extends any[]> = T;

type T1<T extends any[]> = Constraint<MapArray<T>>;
type T2<T> = T extends any[] ? Constraint<MapArray<T>> : never;

Expected behavior: Both T1 and T2 work.

Actual behavior: T1 works, however T2 fails with the following:

Tsc (master):

test.ts:8:43 - error TS2344: Type 'MapArray<T>' does not satisfy the constraint 'any[]'.
  Types of property 'length' are incompatible.
    Type 'T["length"]' is not assignable to type 'number'.

8 type T2<T> = T extends any[] ? Constraint<MapArray<T>> : never;

Playground:

Type 'MapArray<T>' does not satisfy the constraint 'any[]'.
  Type 'MapArray<T>' is missing the following properties from type 'any[]': [Symbol.iterator], [Symbol.unscopables]

Playground Link: here.

Related Issues: Unsure, possibly #21937.

This is of course a simplified example where I've tried to narrow down on the issue itself as much as possible, happy to provide a full example of why I'm trying to use types like this if that helps. Sorry if it's a repost, I did search. 🙂

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions