0

In src/models folder, I have a big list of enums that I put in a namespace Enums.

Enums.ts

namespace Enums {

    const enum MethodType {
       Equal,
       NotEqual,
       ...
    }

}

In src/typings-custom folder, I have some interfaces (among others) that use those Enums.

dto.d.ts

/// <reference path="../models/Enums" />

namespace DTO {

    interface IRule {
       MethodType: Enums.MethodType
    }

}

The only way I found to reference my Enums namespace into the DTO namespace is by using the /// <reference .../> line.

Is it still part of good practices with TypeScript v2.6? Is there another way? (I tried with import but it expects a module instead of a namespace)

Thank you!

2 Answers 2

2

The best way to manage your code is with modules, rather than namespaces. One thing to bear in mind is that it is best not to mix modules and namespaces in TypeScript.

A module is any file with either an import or export expression.

Here is the Enums.ts module:

export const enum MethodType {
    Equal,
    NotEqual,
 }

And an example of importing the MethodType enum for actual use:

import { MethodType } from '../models/Enums';

interface IRule {
    MethodType: MethodType
}

You can also import "the lot", although beware of depending on things you don't need:

import * as Enums from '../models/Enums';

interface IRule {
    MethodType: Enums.MethodType
}

Ideally, you would load your code with a module loader, but you can also use tools to pack the whole lot into a single file if you want to persist with bundling.

Sign up to request clarification or add additional context in comments.

Comments

0

export namespace Enums {
    const enum MethodType {
       Equal,
       NotEqual,
       ...
    }

}


/*-----*/

import * as Enums from './Enums'

Remeber that is better to change name because Enums is similar to a restricted word.

Hope this helped.

2 Comments

1. Yes it helped, thank you! 2. Are you sure Enums (with this case Pascal case) is a restricted word?
Sometimes referring to object types you have to use Pascal case so is best practise to avoid using words similar or equal to restricted ones. However @Fenton implementation is more suitable than this one of mine, so i advise you to use his one!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.