Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript type alias for enum

I have this in my typings file:

declare namespace Somatic {
    enum PropType {
        html,
        object,
        css
    }
}

In another file index.ts, I have a shorter alias for this enum as:

type PropType = Somatic.PropType;

Then I want to use the aliased enum type in a switch statement:

switch (propType) {
    case PropType.html:
        break;
    .
    .
    .
    }

But Typescript does not recognize the aliased enum type values. What is wrong here?

like image 752
prmph Avatar asked Oct 27 '16 04:10

prmph


2 Answers

You should use import keyword instead of type:

import PropType = Somatic.PropType;

More info on import alias declarations here.

** This syntax won't work if you're using babel-plugin-transform-typescript because this is typescript only form of import. Generally using namespaces is not recommended.

like image 76
Aleksey L. Avatar answered Sep 22 '22 23:09

Aleksey L.


In typescript, an enum is both a type and a map. You should alias the type and the map separately:

type PropTypeEnum = Somatic.PropType;
const PropType = Somatic.PropType;
like image 25
joran-le-cren Avatar answered Sep 21 '22 23:09

joran-le-cren