首页 > 解决方案 > 反射元数据“设计:参数类型”在 Deno 上返回未定义

问题描述

我正在尝试使用Deno在 typescript 中实现依赖注入,但是,我遇到了一个问题,Reflect.getMetadata('design:paramtypes', ...)即返回一个undefined值,我不明白为什么。

Deno 1.6.0
Ubuntu 18.04.5 LTS

我使用这个文件作为反映: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

有没有人可以帮助解决这个问题?

标签: typescriptreflectionmetadatadecoratordeno

解决方案


我已经解决了这个问题,将标志-c/--config放在执行命令中:

$ deno run -c tsconfig.json main.ts

推荐阅读