简体   繁体   English

如何在单独的项目中使用自动生成的打字稿声明文件?

[英]how to use auto generated typescript declaration files in separate project?

I have developed typescript library which consists of several classes located in separate ts files. 我已经开发了打字稿库,它由位于单独的ts文件中的几个类组成。

app.ts app.ts

//imported class from separate file    
import leafAdd = require('./Base/leaf');


export class MyClass {
    public leafInstance : leafAdd.Leaf;
}

var MyClassInstance = new MyClass();

The code works fine so i want to use my library in a new separate project, still I do not want to transfer whole code. 该代码工作正常,因此我想在一个新的单独项目中使用我的库,但仍然不想传输整个代码。 Instead I just want to use definition files from typescript compiler (generated by adding --declarations flag). 相反,我只想使用来自Typescript编译器的定义文件(通过添加--declarations标志生成)。 The declaration files look like this: 声明文件如下所示:

app.d.ts app.d.ts

import leafAdd = require('./Base/leaf');

export declare class MyClass {
    public leafInstance : leafAdd.Leaf;
}

When I try to reference this file in a new project it somehow does not work: 当我尝试在新项目中引用此文件时,它不起作用:

newapp.ts 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
    }
}

And I got compiler error saying: "Could not find symbol MyClass" 而且我得到了编译器错误,说:“找不到符号MyClass”

Perhaps I am missing something obvious or just trying to reference the code in a wrong way. 也许我缺少明显的东西,或者只是尝试以错误的方式引用代码。 I would be really gratefull if someone can point me to the right direction. 如果有人能指出我正确的方向,我将不胜感激。

Ok I got it. 好,我知道了。 I should have looked at app.d.ts file for solution :). 我应该已经在app.d.ts文件中寻找解决方案了:)。 Simply, it seems that declaration files containing 简单来说,似乎声明文件包含

export declare class ...

can not be referenced using 不能使用引用

/// <reference path="./path/to/declaration.d.ts" />

and should be imported like any other module/class. 并且应该像其他任何模块/类一样导入。 So working code looks like this: 因此,工作代码如下所示:

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.

相关问题 如何在TypeScript中使用声明文件 - How to use declaration files in TypeScript 如何让 webpack 包含为 Typescript Nodejs(不是浏览器)项目生成的声明文件(以及如何使用) - How to get webpack to include the generated declaration files for a Typescript Nodejs (not browser) project (and how to consume) 如何在 JavaScript 旁边使用 TypeScript 声明文件 - How to use TypeScript declaration files alongside JavaScript 如何在 JavaScript 中使用/导入 TypeScript 声明文件? - How to use/import TypeScript declaration files in JavaScript? TypeScript环境声明和实现在单独的文件中 - TypeScript ambient declaration and implementation in separate files typescript:使用typescript项目中的声明文件全局暴露类型 - typescript: using declaration files in a typescript project to expose types globally 从Visual Studio中的TypeScript引用自动生成的JavaScript文件 - Referencing auto-generated JavaScript files from TypeScript in Visual Studio 我们可以在 Typescript 声明文件中使用全局符号吗? - Can we use global symbols in Typescript Declaration files? 为什么TypeScript要求“声明文件”才能使用外部库? - Why does TypeScript require “declaration files” to use external libraries? 如何使用chakramjs作为打字稿项目? - How to use chakramjs for a typescript project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM