blackHawk blackHawk - 4 days ago 5
Node.js Question

How to use definitly typed files in node/express project

I started with tsd and later typings that we have to install from these sources and provide reference in the server file, but it now we can get declaration files with @types/filename, I don't know why we moved from tsd and typings and now we are at @types.

How can i use these declaration files into my mean project, I have no idea how can I import or what to do to make it functional and get intelligence and type checking.

I have 2 folders in my project, server and client, the client have angular and all frontend stuff, and the server folder(about node) has package.json, server.ts, node_modules and tsconfig files, here is tsconfig file and server.ts: its throwing error can not find module @types/express

{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"typeRoots" : ["./node_modules/@types"],
"moduleResolution": "node"
}
}

import * as express from "@types/express";
var app = express();

app.get('/', function (req, res) {
res.send('Hello World!')
})

app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})


This is package.json:

{
"name": "server",
"version": "1.0.0",
"description": "quiz application backend",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/express": "^4.0.34",
"express": "^4.14.0"
}
}


Thanks!

Answer

With TypeScript 2 you just need to install the typings via npm:

npm install @types/lodash --save

And start using it in your ts file:

import * as _ from "lodash";
_.padStart("Hello TypeScript!", 20, " ");

Intellisense etc. will work, no need for the old /// <reference path="..." /> syntax.

Comments