Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install custom typings?

I created a custom typing contains code that I would like to share across several webstorm node.js projects. The issue is that I am trying to find documentation outlining how to include the typing in a project. I tried to using the npm command but it did not add the folder to the @typings folder under /node_modules folder. Also, when I compile the project that I am trying to add the custom typing to, I am getting duplicate errors for the mongoose library between the project that contains the typing and the project I would like to add the typing to. I am not sure of what the problem could be.

tsconfig.json(for the new type):

{
  "name": "newcustomtype",
  "description": "...",
  "version": "1.0.0",
  "main": "./dist/index.js",
  "typings": "./dist/index.d.ts",
  "scripts": {
    "grunt": "grunt"
  },
  "dependencies": {
    "@types/express": "^4.0.35",
    "@types/express-jwt": "0.0.34",
    "debug": "^2.2.0",
    "dotenv": "^4.0.0",
    "mongoose": "^4.9.2",
    "tslint": "^4.5.1"
  },
  "devDependencies": {
    "@types/mongodb": "^2.1.41",
    "@types/mongoose": "^4.7.9",
    "@types/node": "^7.0.10",
    "grunt": "^1.0.1",
    "grunt-cli": "^1.2.0",
    "grunt-contrib-watch": "^1.0.0",
    "grunt-ts": "^6.0.0-beta.15",
    "grunt-tslint": "^4.0.1",
    "nodemon": "^1.11.0",
    "ts-node": "^3.0.2",
    "tslint": "^4.5.1",
    "typescript": "^2.2.1"
  }
}

tsconfig.json(where the typing should be installed):

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "ES5",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "types": ["reflect-metadata"],
    "lib": ["ES6"],
    "sourceMap": true,
    "inlineSources": true,
    "pretty": true,
    "outDir": "dist",
    "rootDir": "src",
    "noLib": false
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false
}

I tried to follow the doc. on the typescript website but I have not been able to find a resource outlining how to install once created. Although, for it to not install the custom typing, I think there is also a problem with my tsconfig files as well. Please review and let me know what I am missing?

Thanks in advance.

like image 380
user1790300 Avatar asked Sep 01 '17 22:09

user1790300


People also ask

Where do you put custom types in TypeScript?

To do this you should edit the tsconfig. json file, and add the typeRoots property under the compilerOptions property. When the property is excluded, TypeScript is automatically searching for types in the node_modules/@types folder of your project.

What is Typings in TypeScript?

Typings was just a tool to install those files. It is now best practice to just use npm. When you have installed those files, which basically only means downloading them and placing them in your project, the TypeScript compiler will understand* that external code and you will be able to use those libraries.

Could not find a declaration file for module config?

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 .


1 Answers

The node_modules/@types folder is only for definitions which get installed by npm. For custom typings it would be best to specify typeRoots within your tsconfig.json. For example:

{
  [...]
  "compilerOptions": {
    [...]
    "typeRoots" : ["../path/to/typings", "./node_modules/@types"],
    [...]
  }
  [...]
}
like image 129
Stephan Avatar answered Nov 15 '22 09:11

Stephan