-
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
Error when importing untyped JS modules #15031
Comments
/cc: @mhegazy |
Because you have turned on |
oops! you're right that I have I think I can work with the solution you posted on the other thread. Thanks for the quick feedback ! Appreciate it. |
I tried the suggestions mentioned in the linked bug like so: In index.ts i added this code: declare module 'adal-node/*'{
var _a: any;
export = _a;
} and i get this error: |
@balajikris Have you tried: declare module 'adal-node' {
var _a: any;
export = any;
} instead? |
@masaeedu : Thanks for the comment. Yes, I did try that earlier. It also errored out, but a slightly different message. tsc: 'Invalid module name in augmentation. Module 'adal-node' resolves to an untyped module at 'e:/proj/node_modules/adal-node/lib/adal.js', which cannot be augmented.' |
So it's loading the module via the .js file also it seems. Do you have 'allowJs' set in your project config? |
@billti : Thank you for the hint. I tried after setting |
declare module 'adal-node/*'{
var _a: any;
export = any;
} did you mean declare module 'adal-node/*'{
var _a: any;
export = _a;
} ? |
@aluanhaddad -- yes ofcourse. sorry, that was a typo! updated my post |
Is there any way to do something simple like |
Add a declaration for your module, e.g.: declare module "babili-webpack-plugin" {
export ...
} or simply: declare module "babili-webpack-plugin"; |
The issue as described by the OP seems fixed. The other issue described in #15031 (comment), is because a module declaration (i.e |
@mhegazy : I'm still not sure how to correctly use this |
c:\test\15031>npm install adal-node
npm WARN deprecated node-uuid@1.4.7: Use uuid module instead
test@1.0.0 c:\test\15031
`-- adal-node@0.1.22
c:\test\15031>echo import adal from "adal-node"; > a.ts
c:\test\15031>type a.ts
import adal from "adal-node";
c:\test\15031>tsc a.ts |
Interesting, so i do not have to do a I'm on tsc v2.2.2 and I'm seeing |
@balajikris I've just tried it using Typescript v2.2.2 and it works fine for me. Are you sure you're not picking up a stray |
Thanks for checking @TAGC . I do have a .tsconfig but that's intentional and not a stray one. I'll play with this a little more to understand what i'm missing. |
@balajikris probably |
Still not being able to work with adal-node in typescript
my import ts file still says:
|
@oreporan i am unable to reproduce any errors for this scenario. One note, if your import looks like |
Thanks |
@slavafomin it worked for me in a similar case in another project, when I've put a new |
You can put it anywhere as long as tsc can locate it. I.e. using I put them under a |
@Spongman Actually, you can just have a
It automatically makes all js modules any while ts modules keep working as intended. |
I can confirm @SCLeoX's suggestion. I just created a root-level |
@SCLeoX OMG THANK YOU. Hours of pounding my head against outdated docs and absolutely terrible error messages from tsc. If this simple workaround was the front page of http://www.typescriptlang.org then typescript adoption would double and it would also be the most useful thing on that website. |
I've been hunting around for this answer for the better part of a month now. I found a solution that works in VS Code that isn't a wholesale disabling of all validation for javascript/typescript and also did not require that I add files/declarations to a repository that is not mine. Add one or both of these lines to your user settings:
Unlike |
In my case, the |
I took @SCLeoX's suggestion and made the pattern-matching a bit narrower. - declare module '*' {
+ declare module '*.js' {
const value: any
export default value
} |
Is it possible to re-export an existing typed module and use its types for the untyped module? I am importing a library Are there any options for this? I have tried many many many things ......., but this looks most promising, however,
|
Typescript should act smarter in these cases. WHY ON EARTH we need to define a type definition if we explicitly import a JS module. |
i found this to be a helpful write up. https://medium.com/@chris_72272/migrating-to-typescript-write-a-declaration-file-for-a-third-party-npm-module-b1f75808ed2 in my case i just had to place the migration file in |
Two year problem still plagues developers... Below is an example using Angular and Laravel Mix (specific npm scripts) to help anyone else who faces this issue with an untyped package:
The code should transpile correctly. Also to ignore TypeScript errors one can use |
tried all the solutions not working! :( |
Same here |
How would one import this library in typescript? https://www.npmjs.com/package/grudus-timepicker |
Thanks for this, I was racking my brain trying to figure it out. The only other thing I had to do was add the following to
|
In my case I had a module that declares a .d.ts and in that .d.ts they use an implicitly any module, which all the solutions above don't help with, but this did the trick:
|
TypeScript Version: 2.2.1
Steps
"adal-node": "^0.1.22",
topackage.json
and runnpm install
. This is a JS library with no types.index.ts
as belowCode
Expected behavior:
Per this bug's resolution #3019 this code should compile with no errors
Actual behavior:
tsc.exe: 'Could not find a declaration file for module 'adal-node'. 'e:/proj/node_modules/adal-node/lib/adal.js' implicitly has an 'any' type.'
References:
see #3019 and all its linked bugs
The text was updated successfully, but these errors were encountered: