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

About the typeof ES6 import expression #61241

Open
suguanYang opened this issue Feb 21, 2025 · 3 comments
Open

About the typeof ES6 import expression #61241

suguanYang opened this issue Feb 21, 2025 · 3 comments

Comments

@suguanYang
Copy link

suguanYang commented Feb 21, 2025

πŸ”Ž Search Terms

typeof import("x") should be the same as the return type of import expression call

πŸ•— Version & Regression Information

All version

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAKjgQwM5wEoFNkGN4BmUEIcA5FDvmQNwBQuEAdqvJXvALxyiSwAUFKjDIBKenRgBPMFjgBJVAFEAjgFdkAGwA8AFQA0cAKoA+ON11wsADxhYmAE3RG4AfmNXb9p3EvuYUGpyAFxwBFqoIWERWBKSMnKKqhqa2KhqmjAAjOYKyupa2tKyEARw7PiGxVilPODQMIIVIqImNHAA9B3RmpHxsnnJWmkZMABMuUkFOtW12BxVCbW8DU3CYm2d3QFBdHRAA

πŸ’» Code

import * as React from 'react';
const react = import('react');

type IsEqual<T, U> = T extends U ? U extends T ? true : false : false;

πŸ™ Actual behavior

type IsEqualResult1 = IsEqual<typeof react, typeof import('react')>; // false
type IsEqualResult2 = IsEqual<typeof React, typeof import('react')>; // true

πŸ™‚ Expected behavior

type IsEqualResult1 = IsEqual<typeof react, typeof import('react')>; // true
type IsEqualResult2 = IsEqual<typeof React, typeof import('react')>; // false

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

import() has different semantics/types at type and value level. At value level the import() call returns a Promise<>.

@kirkwaiblinger
Copy link

Even after awaiting, there's also differences regarding default vs namespace imports due to TS's ES Module interop... we can get the line to be true like so

@suguanYang suguanYang changed the title Incorrect typeof ES6 import expression About the typeof ES6 import expression Feb 22, 2025
@suguanYang
Copy link
Author

import() has different semantics/types at type and value level. At value level the import() call returns a Promise<>.

I'm trying to make this more clear to me:
The typeof import() expression call is just another form of typeof namespace in types annotation, if a module itself has namespace, it will make the value of import and the type of import at different level.

Is this right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants