Repository: TypeScript
Issue: Type narrowing in checked JS in module scope doesn't work
Labels: Bug

**TypeScript Version:**  3.4.5

**Search Terms:** JS, Narrowing, Module Scope

**Code**

```ts
// In file1.d.ts ======================
declare class Example1 {
    private constructor();
    public has(value: string): boolean;
}

interface Example2 {
    id: string;
    doSomething(key: string): void;
}

interface Example2Static {
    readonly prototype: Example2;
    (id: string): Example2;
}

declare const Example2: Example2Static;

declare const Reader: {
    get: (name: string) => string | boolean | null | Example1 | Example2;
}

// In file2.js ===================
const inModuleScope = Reader.get('approved');

// In JS with allowJs and checkJs on, this DOES NOT narrow
if (inModuleScope instanceof Example1) {
    inModuleScope.has('blah');
} else if (inModuleScope instanceof Example2) {
    inModuleScope.doSomething('hi');
}

(function () {
    // In JS with allowJS and checkJS on, `val` below DOES narrow
    // It's even possible to narrow `inModuleScope`
    const val = Reader.get('approved');
    if (val instanceof Example1) {
        val.has('blah');
    } else if (val instanceof Example2) {
        val.doSomething('hi');
    }
})();
```

**Expected behavior:**
The `inModuleScope` type guards should narrow `inModuleScope` so the two calls are valid inside the if and else-if blocks.

**Actual behavior:**
`inModuleScope` won't narrow unless inside a function. However, it will narrow in TS.

**Playground Link:** [Link, but this won't demonstrate the bug because `checkJS` is needed](https://www.typescriptlang.org/play/index.html#src=%2F%2F%20In%20file1.d.ts%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0D%0Adeclare%20class%20Example1%20%7B%0D%0A%20%20%20%20private%20constructor()%3B%0D%0A%20%20%20%20public%20has(value%3A%20string)%3A%20boolean%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20Example2%20%7B%0D%0A%20%20%20%20id%3A%20string%3B%0D%0A%20%20%20%20doSomething(key%3A%20string)%3A%20void%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20Example2Static%20%7B%0D%0A%20%20%20%20readonly%20prototype%3A%20Example2%3B%0D%0A%20%20%20%20(id%3A%20string)%3A%20Example2%3B%0D%0A%7D%0D%0A%0D%0Adeclare%20const%20Example2%3A%20Example2Static%3B%0D%0A%0D%0Adeclare%20const%20Reader%3A%20%7B%0D%0A%20%20%20%20get%3A%20(name%3A%20string)%20%3D%3E%20string%20%7C%20boolean%20%7C%20null%20%7C%20Example1%20%7C%20Example2%3B%0D%0A%7D%0D%0A%0D%0A%2F%2F%20In%20file2.js%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0D%0Aconst%20inModuleScope%20%3D%20Reader.get('approved')%3B%0D%0A%0D%0A%2F%2F%20In%20JS%20with%20allowJs%20and%20checkJs%20on%2C%20this%20DOES%20NOT%20narrow%0D%0Aif%20(inModuleScope%20instanceof%20Example1)%20%7B%0D%0A%20%20%20%20inModuleScope.has('blah')%3B%0D%0A%7D%20else%20if%20(inModuleScope%20instanceof%20Example2)%20%7B%0D%0A%20%20%20%20inModuleScope.doSomething('hi')%3B%0D%0A%7D%0D%0A%0D%0A(function%20()%20%7B%0D%0A%20%20%20%20%2F%2F%20In%20JS%20with%20allowJS%20and%20checkJS%20on%2C%20%60val%60%20below%20DOES%20narrow%0D%0A%20%20%20%20%2F%2F%20It's%20even%20possible%20to%20narrow%20%60inModuleScope%60%0D%0A%20%20%20%20const%20val%20%3D%20Reader.get('approved')%3B%0D%0A%20%20%20%20if%20(val%20instanceof%20Example1)%20%7B%0D%0A%20%20%20%20%20%20%20%20val.has('blah')%3B%0D%0A%20%20%20%20%7D%20else%20if%20(val%20instanceof%20Example2)%20%7B%0D%0A%20%20%20%20%20%20%20%20val.doSomething('hi')%3B%0D%0A%20%20%20%20%7D%0D%0A%7D)()%3B)

**Related Issues:** None found