我有一个 main.ts 和一个 helper.ts。在 helper.js 中,我尝试覆盖 console.log 并将其导出,以便可以使用 main.js 中的代码,但是当我运行 console.log() 时,它会因以下错误而崩溃。我试图适应这个SO Answer。我在这里想念什么?
我正在使用将 Typescript 文件转换为 JSts-node main.ts
main.ts 中的错误:
ReferenceError:未定义窗口
我试图通过在 helper.ts 的底部添加来解决这个问题:(this as any).window.console = console;
但得到了同样的错误。
助手.ts
export var console = (function(oldCons) {
return {
log: function(text: any) {
oldCons.log("WHEEE" + text);
// Your code
},
info: function(text: any) {
oldCons.info(text);
// Your code
},
warn: function(text: any) {
oldCons.warn(text);
// Your code
},
error: function(text: any) {
oldCons.error(text);
// Your code
}
};
})(window.console);
main.ts
import {console} from './helper.ts'
console.log("hi stack overflow")