Skip to content

Inconsistent treatment of shorthand methods with generic Partial #32416

Closed
@russelldavis

Description

@russelldavis

TypeScript Version: 3.5.1

Search Terms: inconsistent shorthand methods

Code

function assignPartial<T>(target: T, partial: Partial<T>): T {
  return Object.assign(target, partial)
}

let obj = {
  foo(bar: string) {}
}

assignPartial(obj, {foo: (...args) => {}})
assignPartial(obj, {foo(...args) {}})

Expected behavior:
Both calls to assignPartial are typechecked the same, with no any types or errors.

Actual behavior:
In the second call, args is given the type any[], causing noImplicitAny to fail.

Additional notes:

  • If you change Partial<T> to just T, the error goes away (playground link).
  • If you keep Partial but hardcode the type instead of using generics, the error also goes away (playground link).

Playground Link:
https://www.typescriptlang.org/play/#code/ATBmFcDsGMBcEsD2lgEMDO74HNIAVUAnBVAGwB4AVAPgApYjsBTWALmEoBpgAHIk0uwLF4ZKtQCU7SsADeAKBAhCLcIRQB5AEYArJnAB0GLLnqMW3PiLITFwAL527pFsES7gAXjl2QoRIi0WkTs6LCE8JDYEnKOII52xjj4-KKktO463LL+iOy0BoWM6DGe1LH2tiBJuMICGbrZuQVFhNglFbZAA

Related Issues: #11062, but it's a different inconsistency (and has been fixed).

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