Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling ambient types #54123

Closed
5 tasks done
mrmckeb opened this issue May 4, 2023 · 4 comments
Closed
5 tasks done

Allow disabling ambient types #54123

mrmckeb opened this issue May 4, 2023 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@mrmckeb
Copy link

mrmckeb commented May 4, 2023

Suggestion

I understand that this is something that there will be a range of opinions on.

What I'd like is an option to restrict ambient types so that even if a library has shipped with ambient types, they aren't exposed. Ideally, this would also prevent those libraries from modifying global types.

🔍 Search Terms

I searched for:

  • Ambient types
  • Global types

Related:

Possibly related:

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

An option in TS Config to prevent library-provided ambient types from polluting the global namespace.

📃 Motivating Example

The rationale for this:

  • They don’t feel like a good fit in modern module-based JavaScript, where globals are discouraged.
  • They create confusion - “Where is this type coming from? Is it the right type?”
  • They create inconsistency - ex. some engineers import type from React and some rely on the ambient types.
  • There is a high chance of collisions or confusingly similar names in the global space.

Further, I commented on this issue (related) and noted that we've actually encountered bugs because some of these libraries also modify global types.

💻 Use Cases

As an example, we hit an issue last year where global types were being modified by libraries, which caused type issues that were hard to diagnose and fix.

@MartinJohns
Copy link
Contributor

Sounds like a duplicate of #50424.

@RyanCavanaugh
Copy link
Member

It's not really clear how you could make this work in a general case.

Consider the case where you have some generic type with a type constraint:

type F<T extends Foo> = ...

Let's say the library code instantiates F with a global type that you've augmented to match Foo, but which previously wouldn't have matched. Presumably, now this has to be a type error, but it shouldn't be an error because it's code that depends on an invariant that the library itself set up in the first place. That seems really awkward and also not possible for you to fix on your side.

We've looked at the problem of global pollution a lot and not yet found any solutions that don't incur these or other similar kinds of problems. See also #31894 and related discussions

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 4, 2023
@mrmckeb
Copy link
Author

mrmckeb commented May 5, 2023

Thanks, I'm happy to close this off as a duplicate if you'd like.

I realise that this is a hard problem to solve, and I understand the issues that you've raised here.

Thinking about other rules with similar problems, exactOptionalPropertyTypes comes to mind. I've seen a lot of libraries updating code to support that option, and library authors seem very positive/receptive to those requests. If the error could prompt users to ask for/contribute a fix in these scenarios, that would help too.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants