简体   繁体   English

如何在定义文件中引用Typescript枚举

[英]How do I reference a Typescript enum inside a definition file

I am using Visual Studio 2013 with update 4 and Typescript 1.3 installed. 我正在使用安装了更新4和Typescript 1.3的Visual Studio 2013。

If I have a typescript file, like so: 如果我有一个打字稿文件,如下所示:

MyEnums.ts: MyEnums.ts:

export = MyEnumModule;
module MyEnumModule {
    export enum AnEnum { RED, BLUE, GREEN }
}

And I have a definitions file like so: 我有一个像这样的定义文件:

MyDefinitions.d.ts: MyDefinitions.d.ts:

declare module MyDefinitions {
    interface ISomeInterface {
        aProperty: string;
        aMethod: () => void;
        aColor: MyEnumModule.AnEnum;
    }
}

I basically get an error of "Cannot find name 'MyEnumModule'" 我基本上得到一个错误“找不到名字'MyEnumModule'”

This enum file works fine when referenced from typescript files. 从typescript文件引用时,此枚举文件可正常工作。 For instance: 例如:

SomeCode.ts: SomeCode.ts:

export = MyCode;

import MyEnums = require('MyEnums');

module MyCode{

    export class MyClass implements ISomeInterface {
        public aColor: MyEnums.AnEnum = MyEnums.AnEnum.RED;
        ...and so on

My understanding is that adding either /// <reference ... or an import will not work for a .d.ts file (I tried just to be sure and it didn't appear to work either way). 我的理解是添加/// <reference ...或导入将不适用于.d.ts文件(我试图确保它看起来无论如何都不起作用)。

Does anyone know how to reference an enum in a definition file like this? 有谁知道如何在这样的定义文件中引用枚举?

Thanks in advance. 提前致谢。

--Update: --update:

Here is the error I see after trying Steve Fenton recommendations below (with a sample project I just made). 这是我在尝试下面的Steve Fenton建议后看到的错误(我刚刚制作了一个示例项目)。

MyDefinitions.ts : MyDefinitions.ts

import MyEnumModule = require('../App/MyEnums');

declare module MyDefinitions {
    interface ISomeInterface {
        aProperty: string;
        aMethod: () => void;
        aColor: MyEnumModule.AnEnum;
    }
}

MyEnums.ts : MyEnums.ts

export = MyEnumModule;

module MyEnumModule {
    export enum AnEnum { RED, BLUE, GREEN }
}

MyClass.ts : MyClass.ts

export = MyCode;
import MyImport = require('MyEnums');
module MyCode {
    export class MyClass implements MyDefinitions.ISomeInterface {
        public aColor: MyImport.AnEnum = MyImport.AnEnum.RED;
        constructor() { }
        aProperty: string = "";
        aMethod: () => void = null;       
}

} }

Folder structure : 文件夹结构
App 应用

  • -MyClass.ts -MyClass.ts
  • -MyEnums.ts -MyEnums.ts

  • Defintions Defintions
  • -MyDefintions.d.ts -MyDefintions.d.ts
  • Inside MyClass.ts MyDefinitions.ISomeInterface is underlined in red with hover warning "Cannot find name MyDefinitions". 在MyClass.ts内部MyDefinitions.ISomeInterface以红色下划线,并带有悬停警告“无法找到名称MyDefinitions”。

    I have AMD set for the project 我有AMD为该项目设置

    Does anyone know how to reference an enum in a definition file like this? 有谁知道如何在这样的定义文件中引用枚举?

    There are workaround as Steve Fenton pointed out, but the system isn't designed for this. 史蒂夫芬顿指出,有一些解决方法,但系统不是为此而设计的。 You should reference other definition files in your definition file and not reference an *implementation file * ( MyEnum.ts ) in a definition file . 你应该参考其他定义文件在你的定义文件 ,而不是引用*实现文件*( MyEnum.ts定义文件 )。

    I had a check on this and the following definition works for me, although I must admit I have never referenced "actual" code from "definition" code - but I can't think of any reason not to. 我对此进行了检查,以下定义对我有用,虽然我必须承认我从未在“定义”代码中引用“实际”代码 - 但我想不出任何理由不这样做。

    import MyEnumModule = require('MyEnumModule');
    
    declare module MyDefinitions {
        interface ISomeInterface {
            aProperty: string;
            aMethod: () => void;
            aColor: MyEnumModule.AnEnum;
        }
    } 
    

    On mixing definitions and real implementations... 关于混合定义和实际实现......

    The type system in TypeScript is a design time and compile-time tool. TypeScript中的类型系统是一个设计时和编译时工具。 When the type information is constructed at design time it makes no difference whether the type information is inferred from implementation code, taken from annotations that decorate implementations or come from an ambient declaration. 当在设计时构造类型信息时,类型信息是从实现代码推断出来,从装饰实现的注释中得到的还是来自环境声明也没有区别。

    There are many use cases for mixing implementation code and ambient declarations - if you are migrating a million-line JavaScript program to TypeScript, you may not be able to migrate it from the bottom-most dependency upwards. 混合实现代码和环境声明有很多用例 - 如果要将百万行JavaScript程序迁移到TypeScript,则可能无法将其从最底层的依赖项向上迁移。 Also, you can place ambient declarations inside of normal files - not just definition files - if you have a large program, you may not even know whether a type you place in an ambient declaration is "real" or "ambient". 此外,您可以将环境声明放在普通文件中 - 而不仅仅是定义文件 - 如果您有一个大型程序,您可能甚至不知道您在环境声明中放置的类型是“真实”还是“环境”。

    The only difference between implementation code types and ambient declaration types is that the type information is right next to the implementation in real code files, and in a separate file for ambient declarations. 实现代码类型和环境声明类型之间的唯一区别是类型信息紧挨着实际代码文件中的实现,并且位于单独的环境声明文件中。

    So... if you are having a problem using real implemented types in your ambient declaration, it is most likely caused by something that can be fixed. 所以...如果你在环境声明中使用真正实现的类型时遇到问题,很可能是由可以修复的东西引起的。 The example I supplied above works in a project I have in Visual Studio 2013, Update 4 - with TypeScript build configuration set to compile AMD modules. 我上面提供的示例适用于我在Visual Studio 2013,Update 4中的项目 - 使用TypeScript构建配置来编译AMD模块。 If you can share the exact details of the problem, I'm happy to help you get it working. 如果您可以分享问题的确切详细信息,我很乐意帮助您实现它。

    Having said this - if you are creating a type definition for trivial amounts of code, pasting them into a .ts file may even be faster than writing the definition - so you should make a case-by-case decision on where to spend the effort. 说完这个 - 如果你要为大量代码创建一个类型定义,将它们粘贴到.ts文件中甚至可能比编写定义更快 - 所以你应该根据具体情况决定在哪里花费精力。

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM