Say I've got an enum declared in one file (test1.ts):
export enum Colors{
red=1,
blue=2,
green=3
}
Then in another file (test2.ts) I'm declaring a class which has a method. One of the parameters to that method is a Color defined in the Colors enum:
'use strict';
declare var require: any;
declare var exports: any;
var Colors = require('Colors');
class DoSomethingWithColor{
ColorFunction(aColour:Colors){
//Funky color processing here..
}
}
However, I'm getting an error:
Cannot fine name Colors
Even though its exported and required in the second file. Am I doing something wrong here or is this just not a 'typescripty' way of doing what I'm trying to do (and if so, what is the preferred way?)?
Thanks
In TypeScript, enums, or enumerated types, are data structures of constant length that hold a set of constant values. Each of these constant values is known as a member of the enum. Enums are useful when setting properties or values that can only be a certain number of possible values.
Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript. Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums.
Iterate through a typescript enum to get all valuesYou can also iterate through all possible enum values.
Enums in TypeScript isn't only a compile-time feature. The enum type does actually gets compiled into a JavaScript object. This program when compiled produces the following output.
We wrapped the name of the enum in curly braces when importing it - this is called a named import. TypeScript uses the concept of modules , in the same way that JavaScript does. In order to be able to import an enum from a different file, it has to be exported using a named or default export.
We wrapped the name of the enum in curly braces when importing it - this is called a named import. TypeScript uses the concept of modules , in the same way that JavaScript does.
Heterogeneous enums are enums that contain both string and numeric values. Enum in TypeScript supports reverse mapping. It means we can access the value of a member and also a member name from its value. Consider the following example.
Using an enum is simple: just access any member as a property off of the enum itself, and declare types using the name of the enum: // ... respond ("Princess Caroline", UserResponse. Yes ); Numeric enums can be mixed in computed and constant members (see below) .
As was mentioned by jonrsharpe in the comments you have to use one of the typescript supported import statements.
import * from './test1' as Colors
or
import {Colors} from './test1'
For more information on import statements and best practices regarding modules (now namespaces) in typescript, please check out their documentation: https://www.typescriptlang.org/docs/handbook/modules.html https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html
Generally if you are using export/import statements a module loader like CommonJS or WebPack will be required. These programs bundle your code and are responsible for ensuring that dependencies are available to the importer at runtime. Judging by the fact that the import statement worked out of the box, it is likely that you were already using a module loader.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With