microsoft / microsoft/TypeScript

Decorated function parameter types are not inferred

Đang mở
#54,652 2 bình luận 10 reaction 1 người được giao Xem trên GitHub

@rbuckton đang làm issue này rồi.

Từ ngày 19/6/2023.

Needs Investigation
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.