Skip to content

Conditional types break mapped types on arrays and tuples #29702

@falsandtru

Description

@falsandtru

cc @sandersn

TypeScript Version: 3.4.0-dev.20190131

Search Terms:

Code

type Remap1<T> = { [P in keyof T]: Remap1<T[P]>; };
type Remap2<T> = T extends object ? { [P in keyof T]: Remap2<T[P]>; } : T;
  
type a = Remap1<string[]>;
type b = Remap2<string[]>;

Expected behavior:

a type and b type are:

any[]

But this is also wrong. Must be string[].

Actual behavior:

b type is:

{
    [x: number]: string;
    length: number;
    toString: {};
    toLocaleString: {};
    pop: {};
    push: {};
    concat: {};
    join: {};
    reverse: {};
    shift: {};
    slice: {};
    sort: {};
    splice: {};
    unshift: {};
    indexOf: {};
    lastIndexOf: {};
    every: {};
    ... 13 more ...;
    includes: {};
}

Playground Link: http://www.typescriptlang.org/play/index.html#src=type%20Remap1%3CT%3E%20%3D%20%7B%20%5BP%20in%20keyof%20T%5D%3A%20Remap1%3CT%5BP%5D%3E%3B%20%7D%3B%0Atype%20Remap2%3CT%3E%20%3D%20T%20extends%20object%20%3F%20%7B%20%5BP%20in%20keyof%20T%5D%3A%20Remap2%3CT%5BP%5D%3E%3B%20%7D%20%3A%20T%3B%0A%20%20%0Atype%20a%20%3D%20Remap1%3Cstring%5B%5D%3E%3B%0Atype%20b%20%3D%20Remap2%3Cstring%5B%5D%3E%3B

Related Issues:
#29442

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions