我最近使用TS遇到了这个问题,人们可能会意外地访问全局变量(例如窗口属性)。
考虑这个例子:
a.ts
// a.ts
export const postMessage = (message: string) => console.log(message);
b.ts
// Forget to import, but use libs: dom... so it compliles perfectly
// import { postMessage } from './a';
postMessage('hello'); // Oooops
看来,tslint不能也无意处理这种情况。说,TS处理它。 但可能是TS没有...
IMO,期望的行为是禁止隐式访问全局(全局/窗口属性)
postMessage() // Error
window.postMessage() // OK
它认为这是eslint no-undef规则的作用。
似乎TS回购中有一个未解决的问题,但它很陈旧(Microsoft/TypeScript#14306)
如果您知道要解决此问题的任何食谱/信息,请分享。 或者也许我会忘记一些东西并且它很容易修复(因此,愚蠢的问题)