Closed
Description
TypeScript Version: 2.8.0-dev.20180314
Search Terms: global declarations, ambient context.
Code
a.ts
declare global {
interface Window {
someGlobalVar: number;
}
}
window.someGlobalVar = 10;
// to turn on "module"-mode for this file
export const unusedExport = 100500;
entry.ts
import './a';
console.log(window.someGlobalVar); // 10 as expected
entry2.ts
console.log(window.someGlobalVar); // `undefined`, but type is `number`
tsconfig.json
{
"compilerOptions": { "strict": true },
"files": ["entry.ts", "entry2.ts"]
}
Expected behavior:
Error in entry2.ts
about undeclared member.
Actual behavior:
No errors.
If we comment out the import './a';
in entry.ts
, compilation fails with the following errors in BOTH modules (as expected):
entry.ts(2,20): error TS2339: Property 'someGlobalVar' does not exist on type 'Window'.
entry2.ts(1,20): error TS2339: Property 'someGlobalVar' does not exist on type 'Window'.
But if we import a.ts
in the first entry file, both errors disappear, so, using the import in the first entry module produces side effects on the second one. This is a bit surprising that declare global
produces such side effects even when source file is in "module" mode. It leads to the mismatch between declared types and the other side effects like window.someGlobalVar = 10
.