How to reference a Typescript enumeration inside a definition file

I am using Visual Studio 2013 with Update 4 and Typescript 1.3.

If I have a Typescript file, for example:

MyEnums.ts:

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

And I have a definition file like:

MyDefinitions.d.ts:

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

Basically I get the error message "I can not find the name" MyEnumModule "

This enumeration file works fine when linking from Typescript files. For example:

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 

I understand that adding either /// <reference ... or import will not work for the .d.ts file (I tried to just be sure, and it didn't seem to work anyway).

Does anyone know how to reference an enumeration in a definition file like this?

Thanks in advance.

- Update:

Here is the error that I see after you tried the recommendations of Steve Fenton below (with the example I just made).

MyDefinitions.ts

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

MyEnums.ts

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

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 :
The app

-MyClass.ts -MyEnums.ts
DEFINITIONS -MyDefintions.d.ts

Inside MyClass.ts, MyDefinitions.ISomeInterface is highlighted in red with a warning about the hang "Cannot find the name MyDefinitions."

I have AMD installed for the project

+7
enums visual-studio-2013 requirejs amd typescript
source share
2 answers

I had a check for this, and the following definition works for me, although I must admit that I never referenced the "actual" code from the "definition" of the code, but I can’t think of any reason. >

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

About related definitions and real implementations ...

The type system in TypeScript is development time and compilation time. When type information is built during development, it does not matter if the type information is derived from the implementation code, taken from annotations that adorn implementations or come from an ambient declaration.

There are many options for using implementation code and ambient declarations - if you port the program to the millionth version of JavaScript in TypeScript, you cannot transfer it from the bottom dependency up. In addition, you can place ambient declarations inside regular files, not just definition files — if you have a large program, you don’t even know if the type that you place in the ambient declaration is “real” or “surrounding”.

The only difference between the types of implementation code and the types of environmental declaration is that the type information is located next to the implementation in real code files and in a separate file for ambient declarations.

So ... if you have a problem using the actual implemented types in your ambient declaration, this is most likely due to something that can be fixed. The example above works in a project that I have in Visual Studio 2013, Update 4 - with a TypeScript configuration configuration to compile AMD modules. If you can share the exact details of the problem, I am happy to help you make it work.

Having said that - if you create a type definition for trivial amounts of code, pasting them into a .ts file may even be faster than writing a definition, so you have to make individual decisions about where to spend your energy.

+2
source share

Does anyone know how to reference an enumeration in a definition file like this?

There is a workaround, as Steve Fenton pointed out, but the system is not designed for this. You must reference other definition files in the definition file and not refer to the * * implementation * file ( MyEnum.ts ) in the definition file.

+4
source share

All Articles