0

I would like to use this way when I'm writing app.ts file. There are some issues concerning .d.ts imports, I don't get full api exposed of express. The issue is that I don't get full autocompletion in Webstorm2016 (haven't tried it vscode - not relevant for me for now).

///<reference path="../../typings/main.d.ts"/>
import * as express from 'express';
import * as logger  from 'morgan';
import * as bodyParser from 'body-parser';
import {join} from 'path';

/* local imports*/
import * as env from '../../tools/tasks';


class Startup {
    private _app: express.Express;
    private _port: number;

    constructor() {
        this._app  = express();
        this._port = env.settings.PORT;

        this.initialize();
        this.setupViews();
        this.listener();
    }

    private initialize() : void {
        // this._app.[use] => not recognized
        this._app.use(logger('dev')); 
        this._app.use(express.static(join(__dirname, 'public')));
        this._app.use(bodyParser.json());
        this._app.use('/', this.router());
    }

    private setupViews() : void {
        // this._app.[engine] => not recognized
        // this._app.[set]    => not recognized
        this._app.engine('.html', require('ejs').__express);
        this._app.set("views", join(__dirname, "views"));
        this._app.set("view engine", "html");
    }

    private router(): express.Router {
        /* routes */
        let index: express.Router = this._app.get('/', (request: express.Request, response: express.Response) => {
            response.render('index', { title: 'This is express initial' });
        });
        return index;
    }

    private listener() : void {
        let server: any =  this._app.listen(this._port, () => {
            var listeningPort: number = server.address().port;
            console.log('The server is listening on port: ' + listeningPort);
        });
    }
}

declare var app = new Startup();

This is my compilation error list:

src/server/app.ts(5,20): error TS2307: Cannot find module 'path'.
src/server/app.ts(8,22): error TS2307: Cannot find module '../../tools/tasks'.
src/server/app.ts(25,13): error TS2339: Property 'use' does not exist on type 'Express'.
src/server/app.ts(26,13): error TS2339: Property 'use' does not exist on type 'Express'.
src/server/app.ts(26,37): error TS2304: Cannot find name '__dirname'.
src/server/app.ts(27,13): error TS2339: Property 'use' does not exist on type 'Express'.
src/server/app.ts(28,13): error TS2339: Property 'use' does not exist on type 'Express'.
src/server/app.ts(32,13): error TS2339: Property 'engine' does not exist on type 'Express'.
src/server/app.ts(32,29): error TS2304: Cannot find name 'require'.
src/server/app.ts(33,13): error TS2339: Property 'set' does not exist on type 'Express'.
src/server/app.ts(33,31): error TS2304: Cannot find name '__dirname'.
src/server/app.ts(34,13): error TS2339: Property 'set' does not exist on type 'Express'.
src/server/app.ts(39,41): error TS2339: Property 'get' does not exist on type 'Express'.
src/server/app.ts(40,13): error TS2339: Property 'render' does not exist on type 'Response'.
src/server/app.ts(46,32): error TS2339: Property 'listen' does not exist on type 'Express'.
src/server/app.ts(54,17): error TS1039: Initializers are not allowed in ambient contexts.

typings.json:

        {
          "ambientDependencies": {
             "body-parser": "registry:dt/body-    parser#0.0.0+20160317120654",
 "express":"github:DefinitelyTyped/DefinitelyTyped/express/express.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
            "morgan": "registry:dt/morgan#1.2.2+20160317120654",
            "node": "registry:dt/node#4.0.0+20160319033040",
            "systemjs": "registry:dt/systemjs#0.18.4+20160316155526"
      }
    }

Actually have found an answer for that issue. For some reason typings stripping the two definitions of:

 - express-serve-static-core/express-serve-static-core.d.ts"
 - serve-static.d.ts

if you install both of them it's starting showing autocompletion.

2 Answers 2

1

I see no typings for the packages in the code you've pasted.

The most reliable way to solve this issue is to use TypeScript Definition Manager to install typings you need.

You also have to provide type references. If you're going to use TypeScript Definition Manger it will look like:

/// <reference path="./typings/main.d.ts" />
Sign up to request clarification or add additional context in comments.

7 Comments

I'm using typings and I do have a ///<reference path="../../typings/main.d.ts"/> and still no results.
I see by the error log that there's no references in the file you pasted.
Sorry mate but I've did it I will resubmit the error log.
Ok. There's "Cannot find module 'path'" that means no node.d.ts is installed. Could you provide typings.json for more details?
I've added the file.
|
0

Try installing type definitions for express, serve-static, express-serve-static-core, mime as well.

typings install dt~node --global --save
typings install dt~express dt~serve-static dt~express-serve-static-core dt~mime --global --save

Theses will help auto completion.

1 Comment

Typings is now depricated. Use npm i --save @types/express or yarn add @types/express.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.