microsoft / microsoft/TypeScript

Decorated function parameter types are not inferred

Open
#54,652 2 comments 10 reactions 1 assignee View on GitHub

@rbuckton is already working on this.

Since Jun 19, 2023.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

When decorating a class method, the argument types are not inferred from the decorator. When using the decorator method directly, without decorator syntax, the types are inferred correctly.

🔎 Search Terms

decorator, infer, type, argument

🕗 Version & Regression Information
  • This happens for decorators introduced in 5.0
⏯ Playground Link

Playground link with relevant code

💻 Code
function decorate(f: (id: number) => void, context?: any): (id: number) => void { return f }

class Foo {
    @decorate
    one(id) { // Parameter 'id' implicitly has an 'any' type.(7006)
        console.log(id as string)
    }

    two = decorate(function two(id) {
        console.log(id as string) // Conversion of type 'number' to type 'string' may be a mistake… (2352)
    })
}

let foo = new Foo();
foo.one("foo")
foo.two("foo") // Argument of type 'string' is not assignable to parameter of type 'number'.(2345)
🙁 Actual behavior

The parameter id for method one is inferred as any

🙂 Expected behavior

The parameter is inferred as number, similar to how it works when calling the decorated function directly for method two.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.