microsoft / microsoft/TypeScript

Decorated function parameter types are not inferred

オープン
#54,652 コメント 2 件 リアクション 10 件 担当者 1 名 GitHub で見る

@rbuckton がすでに取り組んでいます。

2023年6月19日 から。

Needs Investigation
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。