标签: reflect-metadata

在打字稿更新 2.3.4 => 2.4.2 后,我收到编译错误“找不到‘reflect-metadata’的类型定义文件。”

我有一个用 TypeScript 编写的 React / Mobex 应用程序,由 Webpack 1 构建。将 TypeScript 版本从 2.3.4 更新到 2.4.2 后,出现错误

ERROR in C:\myproject\tsconfig.json
error TS2688: Cannot find type definition file for 'reflect-metadata'.
Run Code Online (Sandbox Code Playgroud)

我也尝试过 Typescript 2.7.2,同样的错误。我已经尝试明确提供其输入的路径"paths",尝试安装最新版本的'reflect-metadata',包括全局安装 - 仍然是相同的错误。

tsconfig.json

{
    "compilerOptions": {
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "jsx": "react",
        "lib": ["dom", "es2015.promise", "es6"],
        "module": "commonjs",
        "moduleResolution": "node",
        "noImplicitAny": true,
        //"noUnusedLocals": true,
        "noUnusedParameters": true,
        "outDir": "./dist/",
        "sourceMap": true,
        "suppressImplicitAnyIndexErrors": true,
        "target": "es5",
        "types": ["reflect-metadata"],
        "baseUrl": ".",
        "paths": {
            "react-split-pane": ["./type_fixes/react-split-pane/index.d.ts"],
            "react-dropzone": ["./type_fixes/react-dropzone/index.d.ts"],
            "react-bootstrap-toggle": …
Run Code Online (Sandbox Code Playgroud)

typescript reflect-metadata typescript-typings

5
推荐指数
1
解决办法
5006
查看次数

typeORM: "message": "\"postgres\" 数据库不支持 \"..." 中的数据类型 \"Object\"。"

鉴于以下实体定义:

@Entity()
export class User extends BaseEntity {
  @Column({ nullable: true })
  name!: string | null;
  
  @Column()
  age!: number;
}
Run Code Online (Sandbox Code Playgroud)

出现以下错误:

typeORM:   "message": "Data type \"Object\" in \"User.name" is not supported by \"postgres\" database."

...

name: 'DataTypeNotSupportedError',
  message:
   'Data type "Object" in "User.name" is not supported by "postgres" database.' }
Run Code Online (Sandbox Code Playgroud)

在查看构建时,我看到 TS 发出的元数据将其作为对象进行寻址:

__decorate([
    typeorm_1.Column({ nullable: true }),
    __metadata("design:type", Object)
], User.prototype, "name", void 0);
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

postgresql node.js typescript reflect-metadata typeorm

5
推荐指数
2
解决办法
2251
查看次数

Typescript 不发出装饰器元数据

我有一个打字稿项目,想检查一些对象。所以我安装reflect-metadata、启用experimentalDeoratorsemitDecoratorMetadatatsconfig.json. 然后我有这个代码:

import 'reflect-metadata';
class Bla {
    thing: string;
}
console.log(Bla, Reflect.getMetadata('design:type', Bla, 'thing'));
Run Code Online (Sandbox Code Playgroud)

它输出undefined. 我希望得到String或其他东西。另外,编译后的 Javascript 如下所示:

var Bla = /** @class */ (function () {
    function Bla() {
    }
    return Bla;
}());
console.log(Bla, Reflect.getMetadata('design:type', Bla, 'thing'));
Run Code Online (Sandbox Code Playgroud)

没有用于设置元数据的代码。有趣的是,在我添加自定义装饰器时,我看到了用于设置元数据的代码:

function deco(target, key) { }
var Bla = /** @class */ (function () {
    function Bla() {
    }
    __decorate([
        deco,
        __metadata("design:type", String)
    ], Bla.prototype, "thing", void 0);
    return Bla; …
Run Code Online (Sandbox Code Playgroud)

typescript reflect-metadata

4
推荐指数
1
解决办法
3086
查看次数

何时以及如何将装饰器应用于@angular包中的装饰类

如果我在我的类中使用装饰器,则在导入类时会评估装饰器.这是一个小例子:

@NgModule({ ... })
export class BModule { ... }
Run Code Online (Sandbox Code Playgroud)

透露为:

var BModule = (function () {
    function BModule() {
    }
    BModule = __decorate([  <---------- decorators are applied here
        core_1.NgModule({...})
    ], BModule);
    return BModule;
}());
exports.BModule = BModule;
Run Code Online (Sandbox Code Playgroud)

但是,当在@angular包中应用模块或任何其他装饰器时,输出如下:

var HttpClientModule = (function () {
    function HttpClientModule() {
    }
    return HttpClientModule;
}());
HttpClientModule.decorators = [
    { type: _angular_core.NgModule, args: [{ ... },] },
];
Run Code Online (Sandbox Code Playgroud)

如您所见,装饰器不适用于此处.他们只是保存在decorators酒店.为什么它与我的代码不同?

我问的原因是,在导入我的装饰类时,我希望它应用了装饰器,因此可以使用Reflect:

const providers = Reflect.getOwnMetadata('annotations', BModule);
Run Code Online (Sandbox Code Playgroud)

但是,对于@angular包中的装饰类,它不能以这种方式工作.

decorator typescript reflect-metadata angular

2
推荐指数
1
解决办法
1205
查看次数

为什么反射元数据仅在使用装饰器时有效?

没有装饰器,元数据会丢失-但是为什么呢?

const Baz = () : ClassDecorator => {
  return target => {}
}
class Bar {}
@Baz()
class Foo {
  constructor(bar: Bar) {}
}
console.log(Reflect.getMetadata('design:paramtypes', Foo));
Run Code Online (Sandbox Code Playgroud)

返回[Function: Bar],这很好。但是如果没有@Baz装饰器(实际上不执行任何操作),它将返回undefined。为什么?

typescript reflect-metadata

1
推荐指数
1
解决办法
891
查看次数