Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript @types modules fail to resolve each other

When I use npm to install TypeScript definitions like this

npm install --save @types/express

I cannot use the installed modules as they fail to resolve each other. For example, @types/express requires @types/express-static-server-core, but as @types/express/index.d.ts contains a relative path to express-static-server-core, the module cannot be resolved:

node_modules/@types/express/index.d.ts(16,30): error TS2307: Cannot find module 'serve-static'.
node_modules/@types/express/index.d.ts(17,23): error TS2307: Cannot find module 'express-serve-static-core'.
node_modules/@types/serve-static/index.d.ts(15,26): error TS2307: Cannot find module 'express-serve-static-core'.
node_modules/@types/serve-static/index.d.ts(16,20): error TS2307: Cannot find module 'mime'.

How can I solve this issue? What is the best way to install TypeScript definitions?

As far as I know, typings is deprecated, so I tried to just install the type definitions from @types and then use

tsc --target ES5 --module commonjs index.ts

but it does not work yet. What am I doing wrong?

like image 446
just.me Avatar asked Nov 09 '22 05:11

just.me


1 Answers

On the chance this is related, in one project, I was using generic types with express:

import { ParamsDictionary, Request, ... } from 'express-serve-static-core';
// ...
app.get<ParamsDictionary, UserDetails>(...);

That worked. When I tried to do the same thing in another project that wasn't using generic types:

import { Request, Response } from 'express-serve-static-core';

I was getting 'Unable to resolve path to module 'express-serve-static-core'.

When I changed my import to:

import express, { Request, Response } from 'express';

The problem went away.

like image 65
ChetPrickles Avatar answered Nov 14 '22 21:11

ChetPrickles