如何配置TypeScript以从窗口隐藏所有全局变量,但访问窗口本身?

时间:2019-06-12 16:46:56

标签: javascript typescript scope

如何配置TypeScript以隐藏窗口中的所有全局变量,仅访问窗口本身?

示例:

此代码可以正常工作:

function foo(length: number): void {
  // Console result: A number from this scope.
  console.log(length);
}

但是当我删除参数length时。短毛绒仍然满意。 因为lengthwindow的全局变量。 See docs

function foo(): void {
  // Console result: A number from window. (not expected in this case)
  console.log(length);
}

预期:

function foo(): void {
  // Console result: undefined (or my global variable if exist)
  // And in case the variable does not exist, the linter will cry as expected.
  console.log(length);
}

我想在全局访问中隐藏窗口范围。但是我仍然希望访问窗口对象本身,如果我将其称为:

function foo(): void {
  // This already works. But length itself without window as context should fail.
  console.log(window.length);
}

是否有tsconfig或tslint选项?

1 个答案:

答案 0 :(得分:1)