我在 TypeScript 中使用节点配置。
我有如下代码
config/default.ts::
import {deferConfig} from 'config/defer';
export default {
service: {
url: 'http://localhost',
errorUrl: deferConfig(function() {
console.info('config before defer:', this);
return `${this['service']['url']}/error.html`;
}),
},
};
src/demo.ts:
import config from 'config';
console.info('resulting config: ', config)
当我使用ts-node.
$ npx ts-node ./src/demo.ts
config before defer: { service: { url: 'http://localhost', errorUrl: [Getter] } }
resulting config: Config {
service: { url: 'http://localhost', errorUrl: 'http://localhost/error.html' }
}
但是,如果我运行用 编译的代码tsc,我会得到
$ NODE_CONFIG_DIR=./build/config ./build/src/demo.js
config before defer: {
default: { service: { url: 'http://localhost', errorUrl: [Getter] } }
}
.../build/config/default.js:9
return `${this['service']['url']}/error.html`;
^
TypeError: Cannot read property 'url' of undefined
的值this有一个额外的水平default。
为什么 ts 和 js 代码的行为不一致?如果我更改export default为export =,它适用于 ts 和 js 代码。
但是,文件要求node-config我使用export default.
我应该export =改用吗?