简体   繁体   English

tsconfig 中的路径无法导入类型

[英]paths in tsconfig cannot import the type

I'm working on server side project using TypeScript.我正在使用 TypeScript 进行服务器端项目。 And I defined some types in ooo.d.ts.我在ooo.d.ts中定义了一些类型。 And I set paths in tsconfig.json .我在tsconfig.json中设置了paths But When I try to import the type I defined, It shows the error, Module '"../../@types/express-custom-types"' has no exported member 'CustomError'.但是当我尝试导入我定义的类型时,它显示错误, 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.我在index.d.ts中定义类型,如下所示。

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

And I defined alias in tsconfig.json我在 tsconfig.json 中定义了别名

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

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

And import the type in errorHandlers.ts like this.并像这样导入errorHandlers.ts中的类型。

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

But it shows error Module '"../../@types/express-custom-types"' has no exported member 'ErrorBody'.但它显示错误Module '"../../@types/express-custom-types"' has no exported member 'ErrorBody'.

I don't know what to do..我不知道该怎么办..

declare module... can be used to add or augment declarations of some external module (usually installed through package.json or generated during the "build"). declare module...可用于添加或扩充某些外部模块的声明(通常通过package.json安装或在“构建”期间生成)。

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.但这不是这里的情况,文件/模块是项目的一部分,您可以删除declare module "express-custom-types"包装器。

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 };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM