I'm using Typescript, Redux, and React, all of which are new to me so this probably has a simple fix I'm just not seeing.
I have an actions.ts file which includes an enum like this:
export enum AnimalTypes {
CAT = 'CAT',
DOG = 'DOG'
}
I import it in my reducers.ts file and was trying to use it in a log statement like this:
import {AnimalTypes} from './actions';
export function someFunction() {
// Print the word "CAT"
console.log(AnimalTypes[AnimalTypes.CAT]);
}
I get the following error when I try to launch my site:
TypeError: Cannot read property 'CAT' of undefined
at someFunction (reducer.ts:5)
at combineReducers.js:20
etc.
My IDE doesn't give me any errors and neither does TSLint. I've read a bunch online trying to find the answer but the closest I came was from someone who didn't have the "export" keyword on their enum and I do.
What could be going on here? I'd be grateful for any help.
Thank you.
EDIT: It's not the reverse mapping that it's having a problem with, rather any usage of the enum. That was just the shortest usage I could think of. I'm actually using it in a switch statement:
import {AnimalTypes} from './actions';
export function otherFunction(animal) {
switch (animal.type) {
case AnimalTypes.CAT:
// Do stuff
}
When I build the above code, I get this error Uncaught TypeError: Cannot read property 'CAT' of undefined
someFunctionbeing called fromactions.ts(perhaps indirectly) before the definition ofAnimalTypesexecutes?Uncaught (in promise) TypeError: Cannot read property 'default' of undefinedAt the bottom of the call stack, there were all the enum imports for the specific file, and the one enum that was causing the trouble was, indeed,undefined. After tons of trial and error, moving those imports up to the top of the file made everything work.