Allow type-only imports on ImportEqualsDeclarations#41573
Merged
andrewbranch merged 8 commits intomicrosoft:masterfrom Dec 3, 2020
Merged
Allow type-only imports on ImportEqualsDeclarations#41573andrewbranch merged 8 commits intomicrosoft:masterfrom
andrewbranch merged 8 commits intomicrosoft:masterfrom
Conversation
DanielRosenwasser
approved these changes
Nov 18, 2020
Member
DanielRosenwasser
left a comment
There was a problem hiding this comment.
Looks good, but let's chat about it at a design meeting.
Member
|
Could you also add tests with |
DanielRosenwasser
requested changes
Nov 19, 2020
Member
DanielRosenwasser
left a comment
There was a problem hiding this comment.
Tests for importsNotUsedAsValues
Member
Author
|
All I want is just once to be able to push a change that will pass CI without having run tests locally, just once |
Member
|
Sounds like this is useful enough for us that we don't need a bot action: #39898 |
Member
|
Aw you use your own fork, sad! |
andrewbranch
commented
Dec 1, 2020
f7f9e81 to
e7c5798
Compare
Kingwl
pushed a commit
to Kingwl/TypeScript
that referenced
this pull request
Dec 4, 2020
* 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
This was referenced May 14, 2021
This file contains hidden or 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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
A while back, @gr2m asked me to help take a look at octokit/webhooks.js#245. My summary of the issue is:
--module=es2015because 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 onallowSyntheticDefaultImportsoresModuleInterop, 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 haveesModuleInteropturned on (although it is suppressible withskipLibCheck).In places where they used
AggregateErrorwithout it making its way into the generated type declarations, they were able to turnesModuleInteropoff, 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 extendedAggregateErroras 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. (AnImportTypeNodewouldn’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 theAggregateErrorclass, and would have been allowed in theextendsclause because the class declaration was ambient. The CJS-style import can be allowed to exist even in--module=es2015because it will necessarily be elided due to thetypekeyword.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 theisTypeOnlyparameter last, but it breaks the pattern of node factory function parameters corresponding to syntax order—i.e., thetypekeyword comes before the name identifier when written out, so theisTypeOnlyparameter comes before thenameparameter in the factory function.)Also fixes #41603