Skip to content

Merged type not inferred as a mapped type argument #18354

@dwickern

Description

@dwickern

TypeScript Version: 2.5.x, 2.6.0-dev.20170908

When passed as an argument, T & U evaluates to T, and the type U is discarded.

declare function f<T, Key extends keyof T>(obj: {[K in keyof T]: T[K]}, key: Key): T[Key];

declare const obj: { a: string } & { b: string };
f(obj, 'a');
f(obj, 'b'); // error

Playground link


Workarounds

Specify the type arguments explicitly:

f<typeof obj, keyof typeof obj>(obj, 'b');

An identity type mapping can be used to create an intermediate type

type Fix<T> = {[P in keyof T]: T[P]}

declare const obj: Fix<{ a: string } & { b: string }>;
f(obj, 'a');
f(obj, 'b');

Related issue typed-ember/ember-typings#32

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