Repository: TypeScript
Issue: a way to import from a module without including its globals
Labels: Suggestion,Awaiting More Feedback

# Suggestion

## 🔍 Search Terms
import without global types

## ✅ Viability Checklist

<!--
   Suggestions that don't meet all these criteria are very, very unlikely to be accepted.
   We always recommend reviewing the TypeScript design goals before investing time writing
   a proposal for ideas outside the scope of the project.
-->
My suggestion meets these guidelines:

* [X] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [X] This wouldn't change the runtime behavior of existing JavaScript code
* [X] This could be implemented without emitting different JS based on the types of the expressions
* [X] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
* [X] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).


## ⭐ Suggestion
it would be useful if there was a way to import from a module that has globally declared symbols, without importing those as well.

for example:

```ts
// node_modules/foo/main.d.ts
// i want to import some useful type from this 3rd party module, but it incorrectly assumes it will be run
// in an environment with `value` globally available
declare global {
    const value: string
    type GlobalFoo = {}
}
export type Foo = {}
```

```ts
// main.ts
// my module
import {Foo} from 'foo'
const foo: Foo = {}
const globalFoo: GlobalFoo = {} // no error
console.log(value) // no error
```

here, i only want to be able to use what i imported from the `foo` module. i don't want any of the globally declared stuff

i guess there are two parts to this feature request (or maybe only one of them is needed?):
### 1. a keyword on the import statement to prevent globals from being included in the import

```ts
import noglobals {Foo} from 'foo'
```

### 2. the ability to import globally declared types directly
if you want to be able to use them without importing all of the globals from the module into the global scope:
```ts
import type {GlobalFoo} from 'foo'
```
obviously, this should only be allowed for type imports, as it would otherwise fail at runtime

## 📃 Motivating Example
in playwright, the dom types are imported in order to support writing code that gets executed within the browser - see https://github.com/microsoft/playwright/issues/3037. specifically, this is a workaround for #43434

however, there's no way to disable this without breaking everything - https://github.com/microsoft/playwright/issues/16707

ideally, playwright should be able to import and use the dom types without incorrectly declaring globals like `window` to exist in the nodejs runtime

## 💻 Use Cases
i think there are countless examples in the wild of modules that incorrectly leak browser globals into node and vice versa. here are some recent examples i've come across:
- https://github.com/postmanlabs/postman-app-support/issues/10939
- https://github.com/evanw/esbuild/issues/2388
- https://github.com/vitejs/vite/issues/9813
- https://github.com/benjamine/jsondiffpatch/issues/323