6991httam
6991httam

Reputation: 375

paths in tsconfig cannot import the type

I'm working on server side project using TypeScript. And I defined some types in ooo.d.ts. And I set paths in tsconfig.json. But When I try to import the type I defined, It shows the error, Module '"../../@types/express-custom-types"' has no exported member 'CustomError'.

The project structure is like the below.

├── src
│   └── routes
│       └── errorHandlers.ts
├── tsconfig.json
└── @types
    └── express-custom-types
        └── index.d.ts

I define the types in index.d.ts like the below.

declare module "express-custom-types" {
  export type ErrorBody = { type: string; status: number; message: string };
}

And I defined alias in tsconfig.json

"compilerOptions": {
    ...(omit)...

    "baseUrl": "./",
    "paths": {
      "*": ["*"],
      "@custom-types/*": ["@types/*"],
    },

And import the type in errorHandlers.ts like this.

import { ErrorBody } from "@custom-types/express-custom-types";

But it shows error Module '"../../@types/express-custom-types"' has no exported member 'ErrorBody'.

I don't know what to do..

Upvotes: 1

Views: 1371

Answers (2)

Aleksey L.
Aleksey L.

Reputation: 38046

declare module ... can be used to add or augment declarations of some external module (usually installed through package.json or generated during the "build").

But this is not the case here, the file/module is part of the project and you can just remove declare module "express-custom-types" wrapper.

Upvotes: 1

g2jose
g2jose

Reputation: 1465

You want to make sure that the module you're declaring has the same full name as the one you're trying to import. This should work:

declare module "@custom-types/express-custom-types" {
  export type ErrorBody = { type: string; status: number; message: string };
}

Upvotes: 0

Related Questions