1

I have a Node server created as:

function createHttpServer(app) {
    var http = require('http');
    var server = http.createServer(app);
    return server;
}

I am in the process of migrating our .js code into .ts code and I notice that when the extension is .js, I get type information for createServer. However, when I switch to .ts it becomes any, so I struggle with the correct typings.

This is what my tsconfig.json file looks like:

{
    "compileOnSave": false,
    "compilerOptions": {
        "allowJs": true,
        "outDir": "./out-tsc",
        "sourceMap": true,
        "declaration": false,
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "target": "es6",
        "types": [
            "node"
        ],
        "lib": ["es6"],
        "module": "commonjs",
        "pretty": true,
        "allowSyntheticDefaultImports": true
    }
}

Why, despite the node types, do I get any instead of the correct type information?

2
  • 1
    If you see the type within your create server function, is it correct? Commented Sep 10, 2019 at 16:33
  • Yes, but only when I am using the .js extension. Commented Sep 10, 2019 at 16:39

1 Answer 1

5

Try using import instead of require

import * as http from "http";

function createHttpServer(app) {
  // var http = require("http");
  var server = http.createServer(app);
  return server;
}
Sign up to request clarification or add additional context in comments.

Comments

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.