ウェブサイト検索

typescript - DenoでReflectionメタデータ 'design:paramtypes' がundefinedを返す


私はDenoを使ってTypeScriptで依存性注入を実装しようとしていますが、Reflect.getMetadata('design:paramtypes', ...) が undefined を返してしまう問題が発生しており、その理由がわかりません。

Deno 1.6.0
Ubuntu 18.04.5 LTS

私はこのファイルをReflectとして使用しています: https://github.com/edualb/dependencyInjectionPOC/blob/main/Reflect.ts

tsconfig.json
{
  "compilerOptions": {
    "target": "ES2020",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "lib": ["es5", "es6", "dom"],
  }
}

コード

decorator.ts
export interface Type<T> {
    new(...args: any[]): T;
}

export function Injectable(): (target: Type<any>) => void {
    return (target: Type<any>) => {}
}
service2.ts
import { Injectable } from './decorator.ts';

@Injectable()
export class Service2 {
    constructor() {}
}
service.ts
import { Injectable } from './decorator.ts';
import { Service2 } from './service2.ts';

@Injectable()
export class Service {
    constructor(private s2: Service2) {}
}
main.ts
import { Reflect } from './Reflect.ts';
import { Service } from './service.ts';

console.log(Reflect.getMetadata('design:paramtypes', Service)) // here is returning undefined

実行に使用するコマンド:

deno run main.ts

Github: https://github.com/edualb/dependencyInjectionPOC

この問題を手伝ってくれる人はいませんか?

回答 #1

次のURLを参照してください https://deno.land/[email /advanced/typescript/configuration :

tsconfig.json の代わりに deno.json を使用することが推奨されます…

deno.json の最小の例 :

{    
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "jsx": "react-jsx",
    "jsxImportSource": "preact"
  }
}

すると、Reflect.getMetadata("design:type", target, key); のようなことが機能します。

回答 #2

この問題は、実行コマンドにフラグ -c/--config を付けることで解決しました:

deno run -c tsconfig.json main.ts