@abstraqt-dev/nameof 中文文档教程
这是 TypeScript 编译器的转换器,它添加了一个全新的 nameof 关键字。
WebPack 和 ts-loader 的用法示例:
在 webpack.config.js 中:
1) 导入转换器的名称
const nameOfTransformer = require("./node_modules/@abstraqt-dev/nameof/lib/transformer").default;
2) 在规则
rules: [
{
test: /\.(ts|tsx)$/,
loader: 'ts-loader',
options: {
getCustomTransformers: (program) => ({
before: [nameOfTransformer(program)],
after: []
})
},
exclude: /node_modules/,
},
第 3 节中) 在 tsconfig.json 中将库添加到包含的声明文件
{
...,
"types": [
"@abstraqt-dev/nameof"
],
...
}
中 在您的代码中,现在您可以只使用 nameof 关键字:
interface IFoo {
}
class Bar {
}
let foo = nameof<IFoo>();
let bar = nameof<Bar>();
console.log(foo); // "IFoo"
console.log(bar); // "Bar"
This is a transformer for the TypeScript compiler that adds a whole new nameof keyword.
Example usage with WebPack and ts-loader:
In webpack.config.js:
1) Import the nameof transformer
const nameOfTransformer = require("./node_modules/@abstraqt-dev/nameof/lib/transformer").default;
2) In the rules sction
rules: [
{
test: /\.(ts|tsx)$/,
loader: 'ts-loader',
options: {
getCustomTransformers: (program) => ({
before: [nameOfTransformer(program)],
after: []
})
},
exclude: /node_modules/,
},
3) In tsconfig.json add the library to the included declarations files
{
...,
"types": [
"@abstraqt-dev/nameof"
],
...
}
In your code now you can just use the nameof keyword:
interface IFoo {
}
class Bar {
}
let foo = nameof<IFoo>();
let bar = nameof<Bar>();
console.log(foo); // "IFoo"
console.log(bar); // "Bar"
更多