小编gar*_*rek的帖子

打字稿:禁止隐式引用全局变量

我最近在使用 TS 时遇到了这个问题,其中可能会意外访问全局变量(例如窗口属性)。

考虑这个例子:

a.ts

// a.ts
export const postMessage  = (message: string) => console.log(message);
Run Code Online (Sandbox Code Playgroud)

b.ts

// Forget to import, but use libs: dom... so it compliles perfectly
// import { postMessage } from './a';

postMessage('hello'); // Oooops
Run Code Online (Sandbox Code Playgroud)

看来,tslint 不能也无意处理这个案子。说,TS 处理它。但可能是 TS 不...

IMO,所需的行为是禁止隐式访问全局变量(全局/窗口属性)

postMessage() // Error
window.postMessage() // OK
Run Code Online (Sandbox Code Playgroud)

它认为这就是 eslint no-undef规则所做的。

TS repo 中似乎存在一个未解决的问题,但它已经过时了(Microsoft/TypeScript#14306

如果您知道任何解决此问题的食谱/信息,请分享。或者也许我忘记了一些东西并且很容易修复(因此,愚蠢的问题)

lint typescript tslint

5
推荐指数
0
解决办法
188
查看次数

标签 统计

lint ×1

tslint ×1

typescript ×1