TypeScriptでDecorator function return type~のエラー

タイプスクリプト TypeScript

TypeScriptのコンパイル時に

Decorator function return type 'PropertyDescriptor' is not assignable to type 'void | ((event: Event) => void)'.

というエラーが出てハマったので備忘録として残しておきます。

コードとエラー

function test(_: any, _2: any, descriptor: PropertyDescriptor) {
  const originalF = descriptor.value;
  return originalF;
}

class ProjectInput {
  @test
  private submit(event: Event) {
    console.log(event);
  }
}

省略している部分もありますが、上記のようなコードをコンパイルするとエラーが出ました。

エラー内容はデコレーター関数の戻り値に ‘PropertyDescriptor’ は型に割り当てることができないよと言った感じのエラーみたいです。

対処

以下のように1行目にanyを追加。

function test(_: any, _2: any, descriptor: PropertyDescriptor) : any {
  const originalF = descriptor.value;
  return originalF;
}

コンパイルするとエラーがなくなりました。

 

参考

https://itecnote.com/tecnote/r-typescript-ts1241-unable-to-resolve-signature-of-method-decorator-when-called-as-an-expression/
きせる

タクシー運転手を1年経験し、畑違いのエンジニアに転職。エンジニアに向いていないと思いつつ現在3年目。

きせるをフォローする
スポンサーリンク
TypeScript