Skip to content

Pick<T, xxx> sometimes considered an empty type #29067

@ahejlsberg

Description

@ahejlsberg

TypeScript Version: 3.3.0-dev

Code

function test<T extends { a: string }>(obj: T) {
    let { a, ...rest } = obj;
    return { ...rest, b: a };
}

let o1 = { a: 'hello', x: 42 };
let o2: { b: string, x: number } = test(o1);  // Error, but shouldn't be

Expected behavior:

No error.

Actual behavior:

Error as shown above.

Playground Link:

https://www.typescriptlang.org/play/index.html#src=function%20test%3CT%20extends%20%7B%20a%3A%20string%20%7D%3E(obj%3A%20T)%20%7B%0D%0A%20%20%20%20let%20%7B%20a%2C%20...rest%20%7D%20%3D%20obj%3B%0D%0A%20%20%20%20return%20%7B%20...rest%2C%20b%3A%20a%20%7D%3B%0D%0A%7D%0D%0A%0D%0Alet%20o1%20%3D%20%7B%20a%3A%20'hello'%2C%20x%3A%2042%20%7D%3B%0D%0Alet%20o2%3A%20%7B%20b%3A%20string%2C%20x%3A%20number%20%7D%20%3D%20test(o1)%3B%0D%0A

Related Issues:

Found while investigating #29062.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions