-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow type-only imports on ImportEqualsDeclarations #41573
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 type-only imports on ImportEqualsDeclarations #41573
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, but let's chat about it at a design meeting.
Could you also add tests with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests for importsNotUsedAsValues
All I want is just once to be able to push a change that will pass CI without having run tests locally, just once |
Sounds like this is useful enough for us that we don't need a bot action: #39898 |
Aw you use your own fork, sad! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
f7f9e81
to
e7c5798
Compare
* Allow type-only ImportEqualsDeclarations * Suppress CJS-in-ESM error when type-only * Add grammar error on import type in import alias * Update API baselines * Fix importsNotUsedAsValues with ImportEqualsDeclarations * Make bad error talk words more good for Daniel. Fixes microsoft#41603 * One more error message baseline update * Update transformer and emitter
A while back, @gr2m asked me to help take a look at octokit/webhooks.js#245. My summary of the issue is:
--module=es2015
because it ships ESM to npm--declaration
"aggregate-error"
The combination of (1) and (3) is a problem—CommonJS imports are not allowed with
--module=es2015
. The error message says that you have to turn onallowSyntheticDefaultImports
oresModuleInterop
, which is what the Octokit maintainers did. However, that is a problem for (2), because it just pushes the error onto consumers of the library who don’t haveesModuleInterop
turned on (although it is suppressible withskipLibCheck
).In places where they used
AggregateError
without it making its way into the generated type declarations, they were able to turnesModuleInterop
off, continue using a default import (since it works at runtime—this kind of interop is in the current ESM draft spec, and is implemented in Node now), and suppress the TypeScript error with// @ts-ignore
. However, there was one place where they needed to declare a publicly exposed class that extendedAggregateError
as part of its definition. They ended up simply copying the relevant parts of the class declaration as they had no good, portable way to import it. (AnImportTypeNode
wouldn’t have worked without separately declaring a value side for the constructor, although this might have been slightly less tedious than copying the declaration.) If the syntaximport type AggregateError = require("aggregate-error")
had been allowed, it would have been a perfect fit—it captures both the type and the value side of theAggregateError
class, and would have been allowed in theextends
clause because the class declaration was ambient. The CJS-style import can be allowed to exist even in--module=es2015
because it will necessarily be elided due to thetype
keyword.All the type-checking (and parsing—in order to give a good parse error) mechanics were already enabled for this, so this PR mostly just removes error messages to allow it. It’s obviously not a full solution to the awkward interop story between ESM and CJS, but it’s an additional tool that library authors can use, at next to no implementation cost. The only downside here is the factory API change for
ImportEqualsDeclaration
. (I could improve back-compat by putting theisTypeOnly
parameter last, but it breaks the pattern of node factory function parameters corresponding to syntax order—i.e., thetype
keyword comes before the name identifier when written out, so theisTypeOnly
parameter comes before thename
parameter in the factory function.)Also fixes #41603