Skip to content

Typescript fails to infer return type for function under some circumstances (related to typeof operator)Β #48134

@ashtonsix

Description

@ashtonsix

Bug Report

πŸ”Ž Search Terms

typeof, generic, return type, inference

πŸ•— Version & Regression Information

Tested with TypeScript 4.6.2 only

⏯ Playground Link

Playground link with relevant code (includes a few extra variations on the issue)

πŸ’» Code

let value = 1

function decorate<O extends any>(fn: (first: {value: typeof value}) => O) {
    return (): O => fn({value})
}
let f = decorate(({value}) => 5)

πŸ™ Actual behavior

The type of f is () => unknown.

πŸ™‚ Expected behavior

The type of f should be () => number. If typeof value (from the code example) is replaced with number, then the code behaves as expected. The code also behaves as expected when first is not an object.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix 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