Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to find typings.d.ts and declare modules in angular 2 typescript app

I am new to Typescript and Angular 2. I need to install an npm dependency and use it in my angular 2 app.

The dependency is https://www.npmjs.com/package/ng2-stomp-service

I have installed the necessary packages, but I need to add the following to my typings.d.ts file

declare module 'stompjs';
declare module 'sockjs-client';

I am unable to find the typings.d.ts file in my project.

I have tried the following so far,

npm install typings --global
npm install @types/stompjs
npm isntall @types/sockjs-client
typings install dt~stompjs --save
typings install dt~sockjs-client --save

I have typings.json file with contents,

{
  "dependencies": {
    "sockjs-client": "registry:dt/sockjs-client#1.0.3+20160727010356",
    "stompjs": "registry:dt/stompjs#2.3.0+20161111105645"
  }
}

When I run my angular 2 app with npm start it throws error as follows

ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (27,2): Member 'config' implicitly has an 'any' type.
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (36,2): Member 'queuePromises' implicitly has an 'any[]' type.
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (83,32): Parameter 'str' implicitly has an 'any' type.
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (132,53): Parameter 'response' implicitly has an 'any' type.
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (27,2): Member 'config' implicitly has an 'any' type.
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (36,2): Member 'queuePromises' implicitly has an 'any[]' type.
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (83,32): Parameter 'str' implicitly has an 'any' type.
ERROR in D:/Userfiles/subramanians/projects/hand-cricket/node_modules/ng2-stomp-service/dist/stomp.service.ts (132,53): Parameter 'response' implicitly has an 'any' type.

I am not sure what is causing the issue, I am guessing it is because I have not declared the modules in typings.d.ts

Please advise. Thank you.

like image 231
LINGS Avatar asked Jun 05 '17 21:06

LINGS


People also ask

What is Typings D TS file?

The "d. ts" file is used to provide typescript type information about an API that's written in JavaScript. The idea is that you're using something like jQuery or underscore, an existing javascript library. You want to consume those from your typescript code.

Could not find a declaration file for module events?

The error "Could not find declaration file for module" occurs when TypeScript cannot find the type declaration for a module. To solve the error, install the types for the module by running the command from the error message, e.g. npm install -D @types/module-name .

What is Typings in angular?

Typings is an NPM package to handle the type definitions associated with third-party libraries. This way the tooling will be aware of the types used inside the application. After explaining the typings library, Scott summarizes the different coding syntax options with Angular 2.

What is TS file in angular?

ts: This file is a unit testing file related to app component. This file is used along with other unit tests. It is run from Angular CLI by the command ng test. app.


3 Answers

My two cents to this conversation:

In your tsconfig.json file make sure that you have the right path for your typings.d.ts file

"typeRoots": [ "node_modules/@types", "../src/typings.d.ts" ],

like image 184
Amadeus Sánchez Avatar answered Sep 30 '22 03:09

Amadeus Sánchez


It seems like your Typescript compiler tries to compile files from node_modules directory.

Please make sure you have this excluding rule in your tsconfig.json file:

{ 
  "compilerOptions": {},
  "exclude": [
    "node_modules"
  ]
}

Btw. you need just one of @types and typings. The second one is deprecated, all you need is proper @types packages included in your dev dependencies. Hope it helps!

EDIT: I think there shouldn't be any Typescript files in npm package, so I've created a topic with fix on this repository's github: https://github.com/devsullo/ng2-STOMP-Over-WebSocket/issues/5

like image 25
Daniel Kucal Avatar answered Sep 30 '22 04:09

Daniel Kucal


If you are not using new version of angular you can't find typings.d.ts file in your project. My suggestion is to update your project using angular CLI https://cli.angular.io/

like image 32
Devsullo Avatar answered Sep 30 '22 05:09

Devsullo