Skip to content

keyof operator corrupted if object which contains call signature passed to generic function. #18715

Closed
@brn

Description

@brn

TypeScript Version: 2.6.0-dev.20170922

Code

function test<T>(target: T, name: keyof T) {}

test({ a: 1, b: 2, c: 3, d() {} }, "d");

but below code worked.

function test<T>(target: T, name: keyof T) {}

test({ a: 1, b: 2, c: 3, d: () => {} }, "d");

Probably keyof operator can't iterate over properties if object contains method signature passed as function arguments.
And then, below code worked expectedly.

function test<T>(target: T): keyof T {
  return "a" as any;
}

const k = test({ a: 1, b: 2, c: 3, d() {} });
// typeof k = 'a' | 'b' | 'c' | 'd'

Expected behavior:

Compiled

Actual behavior:

Error occured.
error TS2345: Argument of type '"d"' is not assignable to parameter of type 'never'.

I think this issue caused by getPropertiesOfObjectType of checker.ts not return callSignatures.

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