Skip to content

literal function with structure spread can't be rightly inferred in function's generic type slot #28816

Closed
@zheeeng

Description

@zheeeng

TypeScript Version: 3.2.1

Search Terms:

strucutre spread + generic

Code

function id<T>(input: T): T { return input }

function getFoo ({ foo = 42 }) {
  return foo
}

const newGetFoo = id(getFoo)
const newGetFoo2 = id(function getFoo ({ foo = 42 }) {
  return foo
})

Expected behavior:

newGetFoo and newGetFoo2 should have the same signature

Actual behavior:

newGetFoo is:

({ foo }: {
    foo?: number | undefined;
}) => number

but newGetFoo2 is

 ({ foo }: any) => number

Playground Link:
http://www.typescriptlang.org/play/index.html#src=function%20id%3CT%3E(input%3A%20T)%3A%20T%20%7B%20return%20input%20%7D%0A%0Afunction%20getFoo%20(%7B%20foo%20%3D%2042%20%7D)%20%7B%0A%20%20return%20foo%0A%7D%0A%0Aconst%20newGetFoo%20%3D%20id(getFoo)%0Aconst%20newGetFoo2%20%3D%20id(function%20getFoo%20(%7B%20foo%20%3D%2042%20%7D)%20%7B%0A%20%20return%20foo%0A%7D)%0A

Related Issues:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Contextual TypesThe issue relates to contextual typesFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions