[英]how to use auto generated typescript declaration files in separate project?
我已经开发了打字稿库,它由位于单独的ts文件中的几个类组成。
app.ts
//imported class from separate file
import leafAdd = require('./Base/leaf');
export class MyClass {
public leafInstance : leafAdd.Leaf;
}
var MyClassInstance = new MyClass();
该代码工作正常,因此我想在一个新的单独项目中使用我的库,但仍然不想传输整个代码。 相反,我只想使用来自Typescript编译器的定义文件(通过添加--declarations标志生成)。 声明文件如下所示:
app.d.ts
import leafAdd = require('./Base/leaf');
export declare class MyClass {
public leafInstance : leafAdd.Leaf;
}
当我尝试在新项目中引用此文件时,它不起作用:
newapp.ts
/// <reference path="./typings/app.d.ts" />
export class MyOtherClass{
public myClass: MyClass; //Compiler error: Could not find symbol MyClass
constructor() {
//some other code
}
}
而且我得到了编译器错误,说:“找不到符号MyClass”
也许我缺少明显的东西,或者只是尝试以错误的方式引用代码。 如果有人能指出我正确的方向,我将不胜感激。
好,我知道了。 我应该已经在app.d.ts文件中寻找解决方案了:)。 简单来说,似乎声明文件包含
export declare class ...
不能使用引用
/// <reference path="./path/to/declaration.d.ts" />
并且应该像其他任何模块/类一样导入。 因此,工作代码如下所示:
import myclMod = require('./typings/app') //please notice missing file extension - it confused me at the beginning :)
export class MyOtherClass{
public myClass: myclMod.MyClass;
constructor() {
//some other code
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.