无法读取 Typescript Build 中的枚举定义

Posted

技术标签:

【中文标题】无法读取 Typescript Build 中的枚举定义【英文标题】:Cant read Enum definition on Typescript Build 【发布时间】:2021-06-24 09:36:15 【问题描述】:

我在 npm 中有一个 typescript 包,它将所有定义存储到我的项目中:

index.d.ts

export declare namespace OfferCraft 
    enum Country 
        es,
        it,
        fr,
        uk,
        de
    

    enum Brand 
        amazon,
        eci,
        ldld,
        nvidia
    

    interface Log 
        msg: string
        telegram?: boolean
        title?: string
        color?: string
    

    interface Product 
        sku: string;
        brand: Brand;
        country: Country;
        url: string;
        name: string;
        image: string;
        prime: boolean;
        price?: number;
        reacoprice?: number;
        stock?: number;
    

在我的其他项目中,我安装了我的 npm 包,vscode 可以很好地识别类型,并且我没有任何 lint 错误:

但是当我尝试构建我的项目时,我收到了这个错误:

在这一点上我非常沮丧。我尝试了几个选项都没有成功。

【问题讨论】:

【参考方案1】:

删除declare 关键字,它应该可以工作。 declare 关键字仅在您想要描述运行时存在但还没有类型声明的 javascript 类型时使用(例如,全局变量)。以 declare 开头的命名空间纯粹是声明性的,不会被转译为任何运行时代码 (playground):

// Produces export var OfferCraft;
export namespace OfferCraft 
    enum Country 
        es,
        it,
        fr,
        uk,
        de
    


// Is only present for compile time type safety. No code is transpiled from it.
export declare namespace OfferCraft2 
    enum Country 
        es,
        it,
        fr,
        uk,
        de
    

【讨论】:

以上是关于无法读取 Typescript Build 中的枚举定义的主要内容,如果未能解决你的问题,请参考以下文章

无法读取未定义的属性“insertOnMatch”-TypeScript

错误 TypeError:无法读取未定义的属性(读取“跳过”)Typescript/Api

无法读取未定义的属性,Typescript

错误类型错误:无法读取未定义的属性“切片”---Typescript

TypeError:无法使用 TSLint 和 TypeScript 读取未定义的属性“getFullWidth”

NuxtJS Typescript click.native '无法读取 null 的属性'测试''