This project is read-only.

Using enums in definition files

Topics: General, Language Specification
Apr 9, 2014 at 5:22 PM

I am having an issue with definition files. I want to declare an enum in its own file, interfaces in a separate definition file, and then class types in their own files. I can use the enum directly from my class types and this allows me to type the object appropriately. However, I would like my interface to contain the proper type information as well. I could type it as a number since that is essentially what the enum represents, however I would prefer it to be typed more strongly.

I have created a small sample in the playground (note the playground example does not work, copy each section of code into a separate typescript file in visual studio):

Any thoughts about how to make this work would be greatly appreciated.

Apr 9, 2014 at 8:13 PM
Here's one solution:
enum ShapeType {
    Circle = 9,

export = ShapeType; 

import ShapeType = require("ShapeType");

export interface IShape {
    ID: string;
    Type: ShapeType;

import ShapeType = require("ShapeType");
import ShapeInterfaces = require("ShapeInterfaces");

class ShapeBase implements ShapeInterfaces.IShape {
    ID: string = "";
    Type: ShapeType = ShapeType.Circle;
    constructor() {

export = ShapeBase;
Imports that only import type information will be removed by the compiler, so you can use them to get into .d.ts types without affecting the .js output.