Skip to content

Build better import paths for declaration emit/typeToString from reexports if possible #27340

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

Merged

Conversation

weswigham
Copy link
Member

@weswigham weswigham commented Sep 25, 2018

Also, issue an error on relative/deep node_modules import generation (under moduleResolution: "node") if a better specifier cannot be found (which can happen if your type definitions need to depend on the types of a dependency of a dependency which you do not yet directly depend on). While such paths technically work on the compiling system, the resulting .d.ts file will be non-portable; this can cause issues for package consumers, so it is best to mark it as needing a type annotation to fix the problem.

Fixes #26985

@weswigham weswigham requested review from rbuckton, RyanCavanaugh and a user September 25, 2018 18:05
@weswigham
Copy link
Member Author

@typescript-bot test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 25, 2018

Heya @weswigham, I've started to run the extended test suite on this PR at 4f50766. You can monitor the build here. It should now contribute to this PR's status checks.

@weswigham weswigham added this to the TypeScript 3.2 milestone Oct 5, 2018
@weswigham
Copy link
Member Author

@Andy-MS you wanna look over this?

@weswigham
Copy link
Member Author

@rbuckton done.

@@ -2513,6 +2513,10 @@
"category": "Message",
"code": 2738
},
"The inferred type of '{0}' requires a reference to a module at '{1}' to name, which would likely break on type redistribution. A type annotation is necessary.": {
Copy link
Member

Choose a reason for hiding this comment

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

is {1} the name of the package? Or the module itself?

Copy link
Member

Choose a reason for hiding this comment

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

What about

Generating a declaration for '{0}' requires generating a reference to the module '{1}'; however, this is susceptible to breaking upon distribution. Provide an explicit annotation or add an explicit dependency on '{2}'.

Copy link
Member Author

Choose a reason for hiding this comment

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

From the new test:

The inferred type of 'x' requires a reference to a module at 'foo/node_modules/nested' to name, which would likely break on type redistribution. A type annotation is necessary.

It's the specifier we would generate were we to emit it.

Copy link
Member Author

Choose a reason for hiding this comment

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

We have an accessibility error

"The inferred type of '{0}' references an inaccessible '{1}' type. A type annotation is necessary."

which this message is templated after. I imagine we should use consistent language.

Copy link
Contributor

@rbuckton rbuckton left a comment

Choose a reason for hiding this comment

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

Barring Daniel's feedback and a minor nitpick, this seems fine.

if (links.extendedContainersByFile && links.extendedContainersByFile.has(id)) {
return links.extendedContainersByFile.get(id)!;
let results: Symbol[] | undefined;
if (links.extendedContainersByFile && (results = links.extendedContainersByFile.get(id))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This nesting is unnecessarily complicated. What about this:

let results = links.extendedContainersByFile && links.extendedContainersByFile.get(id);
if (results) return results;

@@ -2525,6 +2525,10 @@
"category": "Error",
"code": 2741
},
"The inferred type of '{0}' requires a reference to a module at '{1}' to name, which would likely break on type redistribution. A type annotation is necessary.": {
Copy link
Member

Choose a reason for hiding this comment

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

Naming the type of '{0}' requires a reference to a module at '{1}' to name, which would likely break on type redistribution. A type annotation is necessary.

or

``
Generating a declaration for the type of '{0}' requires a reference to a module at '{1}' to name, which would likely break on type redistribution. A type annotation is necessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

The inferred type of '{0}' cannot be named without a reference to '{1}'. This is likely not portable. A type annotation is necessary. ?

@weswigham weswigham force-pushed the lookup-reexports-for-better-imports branch from b966829 to 278149c Compare November 13, 2018 21:26
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

Successfully merging this pull request may close these issues.

TypeScript generates invalid import for re-exported types
4 participants