forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: additive global context for npm packages (first pass) #1
Open
dsherret
wants to merge
15
commits into
denoland:branch_v4.8.3
Choose a base branch
from
dsherret:denoNodeTypeChecking
base: branch_v4.8.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 tasks
288f187
to
8e8c37b
Compare
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is hopefully a temporary fork while we investigate npm specifiers in Deno. In the future, we hope a general solution to this problem can be upstreamed to TypeScript. Note the code here is written in a way to make maintaining a fork easier and wouldn't be what would be upstreamed.
Problem A - Need for additive node-specific global context
With npm specifiers, Deno offers a portal into npm modules. So we have "deno code" and "node code" running, which has slightly different behaviour. For example, if you use a
setTimeout
in Node code, it will return aNodeJS.Timeout
, but if you usesetTimeout
in Deno it will return anumber
as in browsers.setTimeout
(different return type).process
global.import * as fs from "fs"
should error in Deno code, but succeed in Node code.import "npm:some-package/set-globals"
which might set globals.@types/node
they want to use, if desired, or some other package.Solution
The solution here is to manually update TypeScript's type checker to have an additional override set of globals for Node along with introducing a
nodeGlobalThisSymbol
to be the symbol forglobalThis
in node files. There is a list of type names that get added to the node code and those symbols stay over there.I'm sure the code as-is today will have issues as I haven't spent much time in TypeScript's codebase. We will investigate and fix anything going forward.
Example types in npm packages
Example types in deno code
Problem B - Need for mapping npm specifier to ambient module
The TypeScript Node ecosystem seems reliant on bare specifier mapping. For example, the stripe npm package provides built-in types, but instead of exporting the types it declares an ambient module:
https://github.com/stripe/stripe-node/blob/deb7bb54c82115d04ad8ba246d8a0ffb0b7c885c/types/2022-08-01/index.d.ts#L130
In my opinion, it's incorrect to do this because then it is impossible to alias the package in a package.json and get its types (ex. doing
"stripe-alias": "npm:stripe@^10.13.0"
in a package.json, then importing"stripe-alias"
will error). That said, several packages do this and if you search DefinitelyTyped for "declare module" you will see many packages that also do this or extend other package's declarations via a bare module specifier.Solution
The solution implemented in this PR is that when a modules resolves to a symbol with zero exports and the specifier is an npm package reference, it will then resolve to the ambient module for that npm package. For example, say someone does
import Stripe from "npm:[email protected]"
, it will resolve to thetypes/2022-08-01/index.d.ts
file, find the symbol has no exports, then fallback to the ambient "stripe" module.This has some obvious issues, like if someone imports from
"npm:stripe@9"
in one part of their code and then imports from"npm:stripe@10"
in another part, but I would suspect a user doing this is a rare scenario.The current implementation is very basic. We'll need to improve this in the future.