node.jstypescriptnode-modules

Everything imported with "import" gets types but require() doesn't


I don't know if this issue is related to VS Code or not. But basically when importing packages in typescript with this syntax

import express from "express";

I get the type definitions along with the code itself. But whenever I use

const express = require("express");

It has the any type. This is not express specific.

I am new to node.js BTW.


Solution

  • If you need to use require to load your modules (because you are in a CommonJS module or for whatever reason), you can still use it with the import statement. It will load the types as well.

    import express = require('express');