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

Support resolveJsonModule in new module modes #46434

Merged
merged 2 commits into from
Oct 27, 2021

Conversation

weswigham
Copy link
Member

Fixes #46362

The bulk of this change is modifying the behavior for esm mode json imports, which no longer have named imports available to them. Such imports are still flagged by a JSON imports are experimental in ES module mode imports diagnostic, however, since esm json imports are experimental. I don't know if we'd want another flag to suppress this error or not, but I think it's an important error to have, since esm mode json imports are still up in the air as to weather they require import assertions or not, and so may need adjustment in the future. In any case, with the behavior updated, the error can always be //@ts-ignore'd if you're using the --experimental-json-modules node flag.

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Oct 19, 2021
Copy link
Member

@andrewbranch andrewbranch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think // @ts-ignore is fine for suppressing this, but want to get @DanielRosenwasser’s input before merging

@DanielRosenwasser
Copy link
Member

As long as this only affects node12 and nodenext this should be fine.

@weswigham
Copy link
Member Author

Yep - the error should only be issued under the new module modes (as they're the only time file/import modes aren't undefined).

@frank-dspeed
Copy link

i am wondering a bit about error TS7062: JSON imports are experimental in ES module mode imports. why is that a error its a info maximum a warning but a error?

@weswigham
Copy link
Member Author

All diagnostics in TS are errors. We don't issue non-errors. Plus, it legitimately doesn't work at runtime under default configuration.

Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good, just a few small formatting/naming requests.

@@ -2637,6 +2637,14 @@ namespace ts {
return usageMode === ModuleKind.ESNext && targetMode === ModuleKind.CommonJS;
}

function isOnlyImportedAsDefault(usage: Expression) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this name include Json somewhere, like isJsonOnlyImportedAsDefault? I thought it might be evident from its usage, but it appears to be used in a place with all kinds of imports.
I also don't understand what 'Only' means here.

Copy link

@frank-dspeed frank-dspeed Oct 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only means in this case that for example

import { version } from './package.json'

does not work only the default works so

import pkg from './package.json'
const { version } = pkg;

only means that no destructuring style named imports are supported.

if (hasDefaultOnly && type && !isErrorType(type)) {
const synthType = type as SyntheticDefaultModuleType;
if (!synthType.defaultOnlyType) {
const type = createDefaultPropertyWrapperForModule(symbol, originalSymbol);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to this PR, but: is this default-wrapper behaviour likely to change as importing JSON stops being experimental?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sandersn nope it should not stop as the behavior will stay there are currently no plans to change that inside nodejs so only default imports are supported for json type imports as the json is always a whole document there is no partial json parser.

@weswigham weswigham merged commit abfd537 into microsoft:main Oct 27, 2021
Copy link

@frank-dspeed frank-dspeed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM+ No current Issues

@weswigham weswigham deleted the resolveJsonModule-module-node branch November 4, 2021 18:24
mprobst pushed a commit to mprobst/TypeScript that referenced this pull request Jan 10, 2022
* Support resolveJsonModule in new module modes

* Formatting feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typescript 4.5 resolveJsonModule should also work with node12 and nodenext module and resolveTypes
6 participants