【问题标题】:Typescript build creates incorrect import path打字稿构建创建不正确的导入路径
【发布时间】:2020-03-20 14:55:52
【问题描述】:

当我运行 tsc.exe 时,我最终会得到指向源文件夹的导入路径。

dist/typescript-angular 中的文件由 swagger-codegen 使用目标语言 typescript-angular 生成。

tsc -d --types node --target es5 --moduleResolution node --sourceMap true  --experimentalDecorators  --rootDir dist/typescript-angular/fes_wecare_appointment/model/ --outDir dist/node-models/fes_wecare_appointment/ dist/typescript-angular/fes_wecare_appointment/model/*.ts 

我最终从源文件夹导入

export declare namespace WecareNewAppointment {
type AppointmentTypeEnum = 'video' | 'audio' | 'chat';
const AppointmentTypeEnum: {
    Video: import("../../../../../../../../../Users/x/projects/x/y/dist/typescript-angular/fes_wecare_appointment/model/slot").Slot.SlotTypeEnum;
    Audio: import("../../../../../../../../../Users/x/projects/x/y/dist/typescript-angular/fes_wecare_appointment/model/slot").Slot.SlotTypeEnum;
    Chat: import("../../../../../../../../../Users/x/projects/x/y/dist/typescript-angular/fes_wecare_appointment/model/slot").Slot.SlotTypeEnum;
};

}

无需过多引用源文件夹中的插槽类型。这就是它的外观。

export declare namespace WecareNewAppointment {
type AppointmentTypeEnum = 'video' | 'audio' | 'chat';
const AppointmentTypeEnum: {
    Video: import("./slot").Slot.SlotTypeEnum;
    Audio: import("./slot").Slot.SlotTypeEnum;
    Chat: import("./slot").Slot.SlotTypeEnum;
};

}

我可以向 tsc 编译器添加 tsconfig 或其他提示吗?

【问题讨论】:

    标签: typescript tsc


    【解决方案1】:

    我肯定会将 tsconfig.json 文件夹添加到您的项目根目录中。并配置以下内容:

    {
        "include": [
            "dist/typescript-angular/fes_wecare_appointment/model/*.ts"
        ],
        "compilerOptions": {
            "types": ["node"],
            "outDir": "dist/node-models/fes_wecare_appointment/",
            "rootDir": "dist/typescript-angular/fes_wecare_appointment/model/",
            "declaration": true,
            "sourceMap": true,
            "experimentalDecorators": true,
            "moduleResolution": "node",
            "target": "es5"
        }
    }
    

    然后你就运行tsc

    这让你想做什么更清楚一点。并且您的配置似乎有点混乱,因为包含的 ts 文件和 js 输出都在同一个 dist driectory 中。并且不清楚rootDir 选项正在实现什么?你真的想要baseUrl吗?

    另外,您最初是如何在 ts 文件中编写这些导入的?

    【讨论】:

    • 文件由swagger-codegen生成。失败的导入语句由 tsc.exe 生成
    • 听起来问题出在大摇大摆的地方。需要查看那些 ts 文件来确定
    • 使用您的 teconfig.json 并运行 ts 版本 3.8.3 然后一切正常。谢谢
    猜你喜欢
    • 2017-03-14
    • 2022-11-05
    • 2015-03-07
    • 1970-01-01
    • 2021-05-03
    • 2021-03-23
    • 2018-04-28
    • 2020-10-16
    • 2021-11-29
    相关资源
    最近更新 更多