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

Could not find declaration when path mapping the root dir #39639

Closed
tobias-huebner opened this issue Jul 17, 2020 · 4 comments
Closed

Could not find declaration when path mapping the root dir #39639

tobias-huebner opened this issue Jul 17, 2020 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@tobias-huebner
Copy link

tobias-huebner commented Jul 17, 2020

root/

  1. test.ts
  2. func.ts

tsconfig.json
"baseUrl": ".", "paths": { "/*": ["./*"] },

if i do import func from "/func" or import func from "func.js" in my test.ts everything works fine. However if i do import func "/func.js" i suddenly get Could not find a declaration file for module ... implicitly has an 'any' type.

Thanks in advance

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jul 17, 2020
@matsuaki-jp
Copy link

When importing a .js file, if the type definition file (d.ts) does not exist, it is implicitly set to any treatment.

If you set "noImplicitAny" to true in tsconfig.json, you get a compile error.

If you do not change this tsconfig.json, create a definition file with the same file name (test.d.ts) .

You can compile in a terminal to generate a definition file as well, with

tsc test.ts -d

I hope your program works well!

@tobias-huebner
Copy link
Author

my func.ts file is already a typescript file. It would seem redundant to create an extra definition file for it.

even if i do it though and now i have the following files in my root directory:

test.ts
func.ts
func.d.ts
func.js

doing import func from "/func.js" still yields the same error.

setting or unsetting no implicit any doesnt seem to do anything in my case.

Thanks again!

@matsuaki-jp
Copy link

Sorry. I misunderstood the problem.

The cause of this error is the file position of the require statement after the transpile is not correct.

The require("/func") after the transpile searches for the system root, so my In the local environment, the compile error is solved by placing the func.ts file directly under the D drive. done.

The solution is to change the file notation to a relative path (./func) from func.ts,

or you need to fix it with webpack or module-alias, where path conversion does not exist in Typescript.

Please see #35351 and #10866.

Thank you!

@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants