簡體   English   中英

打字稿錯誤:TS2304 找不到名稱

[英]Typescript error :TS2304 cannot find name

通過打字稿課程,我遇到了這些無法編譯並給出錯誤 TS2304 的代碼片段。 任何幫助表示贊賞。

文件 ZooAnimals.ts:

namespace Zoo {
    interface Animal {
        skinType: string;
        isMammal(): boolean;
    }
}

文件 ZooBirds.ts:

/// <reference path="ZooAnimals.ts" />
namespace Zoo {
    export class Bird implements Animal {
        skinType = "feather";
        isMammal() {
            return false;
        }
    }
}

編譯文件的命令:

tsc --outFile Zoo.js ZooAnimals.ts ZooBirds.ts

拋出錯誤:

ZooBirds.ts:3:34 - error TS2304: Cannot find name 'Animal'.

3     export class Bird implements Animal {

要跨文件(或更准確地說跨多個namespace聲明)使用接口,它必須被導出(即使它是同一命名空間的一部分)。 這將起作用:

namespace Zoo {
    export interface Animal {
        skinType: string;
        isMammal(): boolean;
    }
}
namespace Zoo {
    export class Bird implements Animal {
        skinType = "feather";
        isMammal() {
            return false;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM