-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
resolution-mode
Feedback
#49055
Comments
Is declare module "someUntypedPackage" assert { "resolution-mode": "import" } {
import type Blah from "someDependentPackage" assert { "resolution-mode": "import" };
export function foo(blah: Blah): string;
} would certainly be helpful.
I do agree with this, like it's not really an assertion, it actively changes the mode. Something like |
Not at present, but we're aware of the potential need. Right now am ambient module is assumed to define the specifier for both resolution modes, which is odd, but consistent, at least. If you break it out into actual module files, you can get the per-mode conditional definitions node allows. |
The problem is, that there is a JSDoc type Using TypeScript nightly this works:
There needs to be a solution for this, and it appears outside of using TypeScript nightly (it's not viable for a published package to expect all users to also update to TypeScript nightly) there is none. Is there a reason that TypeScript couldn't treat |
I build little utility libs written in TypeScript but distributed as CJS packages, and while I truly wish it were practical to migrate everything to ESM and force downstream consumers to migrate as well, there are a few practical roadblocks to this like Node's still-experimental vm modules and the resulting lack of full ESM support / segfaults in Jest and other testing frameworks. Most of the time I can get away with pinning the CJS versions of packages that have since migrated to ESM. Other times I use dynamic imports to bring in ESM packages. Either way, with a well-configured Doing things this way isn't actually a problem with TypeScript for me, since I use But now I want to try using I was pleasantly surprised when
That's a nice warning, usually I have to remember which imports are ESM and which are CJS myself. However, I was confused when I changed it to A bit of Googling and reading through GitHub issues lead me to the Anyways, I considered littering my code with Growing pains of having two partially-compatible module systems, I guess 😄. |
I haven't found a good way to import types from an ESM-only module into a CJS module. This seems to be the best current option, but relies on // @ts-expect-error
import type { LikeConnectionSource, LikeEdgeSource } from './resolvers/LikeConnection.mjs' assert { "resolution-mode": "import" }; Or, require typescript nightly. |
@DanielRosenwasser the concerns that prompted #48644 no longer apply under the new version of the proposal (not that I think that ever really needed to apply to a typespace construct anyway). Do we have a rough forecast from our TC39 meeting on when the proposal may re-advance to stage 3? I imagine when it does (and we support the new keyword), we'll be able to un-nightly this. |
If this syntax (or any syntax – there is a lot of demand), can be used to get exact Maybe that's hard, or out of scope for what you're trying to do here, but it would have lots of happy users currently squeaking by without it by way of using external input transform tools like |
I need to use assert to import types from ESM project into my CJS project and bcs if it I also need to use nightly typescript... import type KeycloakAdminClientRaw from "@keycloak/keycloak-admin-client" assert { "resolution-mode": "require" };
export type KeycloakAdminClient = KeycloakAdminClientRaw;
import type GroupRepresentationRaw from "@keycloak/keycloak-admin-client/lib/defs/groupRepresentation" assert { "resolution-mode": "require" };
export type GroupRepresentation = GroupRepresentationRaw;
import type EventRepresentationRaw from "@keycloak/keycloak-admin-client/lib/defs/eventRepresentation" assert { "resolution-mode": "require" };
export type EventRepresentation = EventRepresentationRaw;
import type AdminEventRepresentationRaw from "@keycloak/keycloak-admin-client/lib/defs/adminEventRepresentation" assert { "resolution-mode": "require" };
export type AdminEventRepresentation = AdminEventRepresentationRaw;
import type UserRepresentationRaw from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation" assert { "resolution-mode": "require" };
export type UserRepresentation = UserRepresentationRaw; As you see I need to reexport all types from library which I use, simply bcs typescript doesn't allow me import types directly at all(assuming if I wanna use stable ts version without assert)... |
Dual packages are a real thing. You run into this problem anytime you want to This just needs to work with resolution-mode assertions, or whatever other syntax you want to throw at it, and without needing to
|
Is there a reason that this is still open? As far as I can tell, this is no longer a nightly-only feature. But if you are still looking for feedback: This issue taught me a new TypeScript feature that I wasn't aware of and it helped to resolve an issue where I converted a project from CJS to ESM and I needed the ability to import a type as CJS in order to stay compatible with a CJS-only library which transitively imports this type as CJS and I need to stay compatible with it. The only other solution would have been to convert the library to emit both CJS and ESM code which would have been possible but would have implied a larger time-effort and I would rather spend that time in a few weeks from now than now. |
For TypeScript 4.7, we pulled back the
resolution-mode
import assertion fromimport type
syntax. This was based in part on feedback around the feature being a bit outside the spirit of import assertion syntax (#48644); however, we also just don't want to add features if we're not 100% convinced that they're going to be used. This was discussed a bit at #48686.So if you try to use
resolution-mode
as followsyou'll get the following error:
Maybe you're using this because you're trying to bridge a CJS/ESM codebase, or maybe you're trying to provide some sort of meta-library that does that. We're not entirely sure!
So if you run into this error, what are you trying to do? What do you need it for? Is there a syntax you'd prefer to use, or is it fine as-is?
The text was updated successfully, but these errors were encountered: