如何在Typescript中使用参数装饰器

问题描述 投票:0回答:1

我想创建一个引擎,将我的自定义变量注入到打字稿中的函数中。

示例:

const TEST = {
    foo: 1,
    bar: "2",
};
const resolveValue = (name: string) => TEST[name];

function hello(@resolveValue("foo") foo: number, @resolveValue("bar") bar: string) {
    console.log(foo);
    console.log(bar);
}

这种技术类似于nestjs库。

我应该使用哪些技术或库?

我已经在Google上搜索过,但没有找到解决方案。

typescript function decorator
1个回答
0
投票

请参阅 参数装饰器 - TypeScript 官方文档

建议将

reflect-metadata
"experimentalDecorators": true
中的
tsconfig.json
一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.