Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add import assertions to type only imports and import types to force the resolution mode of the specifier#47807

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@weswigham
Copy link
Member

@weswighamweswigham commentedFeb 8, 2022
edited
Loading

This PR adds support for a TS-specific assertion on type-only imports and on import type nodes which forces the resolver into eitherrequire orimport resolution mode, allowing access to types which the containing file's default mode would normally make difficult or impossible to access. They look like this:

importtype{RequireInterface}from"pkg" assert{"resolution-mode":"require"};importtype{ImportInterface}from"pkg" assert{"resolution-mode":"import"};exportinterfaceLocalInterfaceextendsRequireInterface,ImportInterface{}exporttypeLocalType=&import("pkg",{assert:{"resolution-mode":"require"}}).RequireInterface&import("pkg",{assert:{"resolution-mode":"import"}}).ImportInterface;

This is essentially a continuation of#47732 that allows resolver configurability for more kinds of type-based imports. Since it's included in this PR, it'sprobably best to go and review & merge that first.

Fixes#47338 by allowing the types of an esm package to be fetched in a cjs file via these modal imports.
Fixes#47248

motss, worstpractice, and alexeden reacted with hooray emojihappycollision, OctoD, and worstpractice reacted with heart emojijaydenseric, ExE-Boss, robpalme, worstpractice, and nokazn reacted with eyes emoji
@typescript-bottypescript-bot added Author: Team For Milestone BugPRs that fix a bug with a specific milestone labelsFeb 8, 2022
@typescript-bottypescript-bot added the Fix AvailableA PR has been opened for this issue labelFeb 10, 2022
@TimvdLippe
Copy link
Contributor

FYI the PR description claims "Fixes#47807", but this PR has number 47807

weswigham and kaznovac reacted with laugh emoji

@weswighamweswigham removed the Fix AvailableA PR has been opened for this issue labelFeb 15, 2022
@weswighamweswigham merged commitea0db9e intomicrosoft:mainMar 2, 2022
@nicolo-ribaudo
Copy link
Contributor

nicolo-ribaudo commentedMar 30, 2022
edited
Loading

This feels really wrong 😕

The proposal explicitly says thatimport assertions must not affect how a module is evaluated or resolved:

[...]
The implementation of HostResolveImportedModule must conform to the following requirements:

  • [...]
  • moduleRequest.[[Assertions]] must not influence the interpretation of the module or the module specifier; instead, it may be used to determine whether the algorithm completes normally or with anabrupt completion.

In other words, if both these two imports succeed they must resolve/evaluate the same file:

import{A}from"pkg"assert{"resolution-mode":"require"};import{A}from"pkg"assert{"resolution-mode":"import"};

The imports assertions proposaleven mentions a possible follow-up proposal that would allow what you need, but it would be a separate proposal (with likely different syntax).

I know that this PR is only aboutimport type so it's technically "just TS" and not JS, but actively going against the JS semantics should be discouraged.

nayeemrmn, MierenManz, DjDeveloperr, KyleJune, and dsherret reacted with thumbs up emoji

@nicolo-ribaudo
Copy link
Contributor

nicolo-ribaudo commentedMar 30, 2022
edited
Loading

https://github.com/tc39/proposal-import-reflection is probably what you need, even if it's not clear yet if it's also meant to affect resolution.

KyleJune reacted with thumbs up emoji

@weswigham
Copy link
MemberAuthor

weswigham commentedMar 30, 2022
edited
Loading

These don't affect runtime and are type-only (your example above doesn't work - you have to sayimport type); it's fine - it's much better than introducing full on new syntax.

KyleJune reacted with thumbs down emoji

@nayeemrmn
Copy link

it's fine - it's much better than introducing full on new syntax.

@weswigham This is wrong, we're using theassert keyword for something that isn't an assertion. Why? You're correct that this is a type-only statement, all the more reason to do literally anything else and not misuse a JS syntax.

There are plenty of other options that make sense withoutcompletely new and ts-only syntax e.g.

// comment directive, nothing new//@ts-resolution=requireimporttype{RequireInterface}from"pkg";
// https://github.com/tc39/proposal-import-reflectionimporttype{RequireInterface}from"pkg"as"resolution=require";
// dangles off the already ts-only partimporttype(resolution=require){RequireInterface}from"pkg";

Please consider reverting this change.

KyleJune and dsherret reacted with thumbs up emoji

@nayeemrmn
Copy link

I've already seen a lot of people confused by import assertions and what they could be used for in runtimes, specifically conflating them with what's proposed inhttps://github.com/tc39/proposal-import-reflection. It would be a shame to see TS fostering that misconception, or perhaps falling victim to it.

KyleJune reacted with thumbs up emoji

@glen-84
Copy link

Why is it:

assert{"resolution-mode":"import"};

(requiring quotes), and not:

assert{resolutionMode:"import"};

?

@weswigham
Copy link
MemberAuthor

Consistency with the triple-slash reference form and a bit of extra syntax encumbrance to make you pause and think if youreally need to use it. :)

glen-84 reacted with confused emoji

@frehner
Copy link

frehner commentedOct 8, 2022
edited
Loading

Would this also hypothetically help solve the issue found at#46213 ? If so, how soon do you think this feature will graduate from nightly?

Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@RyanCavanaughRyanCavanaughRyanCavanaugh left review comments

@andrewbranchandrewbranchandrewbranch approved these changes

@rbucktonrbucktonAwaiting requested review from rbuckton

Assignees

@weswighamweswigham

Labels

Author: TeamFor Milestone BugPRs that fix a bug with a specific milestone

Projects

Archived in project

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

error importing type from esm in commonjs Allow synchronous type-only import of ESM package from CJS

9 participants

@weswigham@TimvdLippe@nicolo-ribaudo@nayeemrmn@glen-84@frehner@andrewbranch@RyanCavanaugh@typescript-bot

[8]ページ先頭

©2009-2025 Movatter.jp