Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript错误:TS2304找不到名称_Typescript_Namespaces - Fatal编程技术网

Typescript错误:TS2304找不到名称

Typescript错误:TS2304找不到名称,typescript,namespaces,Typescript,Namespaces,在学习打字脚本的过程中,我遇到了这些无法编译的代码片段,并给出了错误TS2304。感谢您的帮助 文件ZooAnimals.ts: namespace Zoo { interface Animal { skinType: string; isMammal(): boolean; } } 文件ZooBirds.ts: /// <reference path="ZooAnimals.ts" /> namespace Zoo { ex

在学习打字脚本的过程中,我遇到了这些无法编译的代码片段,并给出了错误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;
        }
    }
}
抛出错误:

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

3     export class Bird implements Animal {

要跨文件(或者更准确地说跨多个
命名空间
声明)使用接口,必须将其导出(即使它是同一命名空间的一部分)。这将有助于:

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

那当然有效——谢谢。edX课程(来自微软)建议使用“///”标记来解决包含动物定义的问题,而不是导出接口。但不知何故,参考路径似乎不起作用。不知道我是否做错了什么,或者typescript的版本只能按照您的建议工作。@rks发现定义可能仍然需要
。但是,
导出
是最需要的。实际上,如果导出接口,它不需要
标记就可以工作。以下是课程中的代码:ZooAnimals.ts
namespace Zoo{interface Animal{//请注意,这里不需要*export*,因为只有来自Zoo命名空间skinType:string;isMammal():boolean;}}
@rks中的实体才能访问此接口。它应该在没有
///
namespace Zoo {
    export interface Animal {
        skinType: string;
        isMammal(): boolean;
    }
}
namespace Zoo {
    export class Bird implements Animal {
        skinType = "feather";
        isMammal() {
            return false;
        }
    }
}