How to export ENUM from NPM module
Asked Answered
M

1

6

I am building an NPM module to import into my projects. For testing, I am using npm link to link to the module I am developing.

I am trying to export an ENUM.

enums.ts

export const enum PlayActions {
startGame = "START_GAME",
displayQuestion = "DISPLAY_QUESTION",
clearQuestion = "CLEAR_QUESTION",
displayFollowUp = "DISPLAY_FOLLOWUP",
endGame = "END_GAME",
addPlayer = "ADD_PLAYER",
removePlayer = "REMOVE_PLAYER",
disconnected = "DISCONNECTED",
resetGame = "RESET_GAME"
}

index.ts -

import Plearnit, { ConnectionTypes } from './Plearnit';
import { PlayActions } from './enums';
import ActionDTO from './dto/ActionDTO';
import AnswerDTO from './dto/AnswerDTO';
import GameDTO from './dto/GameDTO';
import LibraryDTO from './dto/LibraryDTO';
import PlayLoginDTO from './dto/PlayLoginDTO';
import QuestionDTO from './dto/QuestionDTO';


export { Plearnit, PlayActions, ConnectionTypes, ActionDTO, AnswerDTO, GameDTO, LibraryDTO, 
PlayLoginDTO, QuestionDTO }

After traspiling, I get the following files:

index.js -

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Plearnit_1 = __importDefault(require("./Plearnit"));
exports.Plearnit = Plearnit_1.default;
const ActionDTO_1 = __importDefault(require("./dto/ActionDTO"));
exports.ActionDTO = ActionDTO_1.default;
const AnswerDTO_1 = __importDefault(require("./dto/AnswerDTO"));
exports.AnswerDTO = AnswerDTO_1.default;
const GameDTO_1 = __importDefault(require("./dto/GameDTO"));
exports.GameDTO = GameDTO_1.default;
const LibraryDTO_1 = __importDefault(require("./dto/LibraryDTO"));
exports.LibraryDTO = LibraryDTO_1.default;
const PlayLoginDTO_1 = __importDefault(require("./dto/PlayLoginDTO"));
exports.PlayLoginDTO = PlayLoginDTO_1.default;
const QuestionDTO_1 = __importDefault(require("./dto/QuestionDTO"));
exports.QuestionDTO = QuestionDTO_1.default;

index.d.ts -

import Plearnit, { ConnectionTypes } from './Plearnit';
import { PlayActions } from './enums';
import ActionDTO from './dto/ActionDTO';
import AnswerDTO from './dto/AnswerDTO';
import GameDTO from './dto/GameDTO';
import LibraryDTO from './dto/LibraryDTO';
import PlayLoginDTO from './dto/PlayLoginDTO';
import QuestionDTO from './dto/QuestionDTO';
export { Plearnit, PlayActions, ConnectionTypes, ActionDTO, AnswerDTO, GameDTO, LibraryDTO, 
PlayLoginDTO, QuestionDTO };

enums.d.ts -

export declare const enum PlayActions {
startGame = "START_GAME",
displayQuestion = "DISPLAY_QUESTION",
clearQuestion = "CLEAR_QUESTION",
displayFollowUp = "DISPLAY_FOLLOWUP",
endGame = "END_GAME",
addPlayer = "ADD_PLAYER",
removePlayer = "REMOVE_PLAYER",
disconnected = "DISCONNECTED",
resetGame = "RESET_GAME"
}

When I try to import it into my project, all the imports work correctly except the enum:

import Plearnit, { QuestionDTO, GameDTO, PlayActions } from 'plearnit-connector';

I've tried using and not using 'const'. When I don't use CONST, PlayActions shows up in index.js, but still cannot be imported. I read that you should make them a CONST when exporting, but still no luck.

How can I export my ENUMS so they are importable in my projects?

Marriageable answered 25/3, 2020 at 17:51 Comment(2)
did you find the right way to do this?Actinology
I believe I just moved them inside a class.Marriageable
P
0
export enum PlayActions {
  startGame = "START_GAME",
  displayQuestion = "DISPLAY_QUESTION",
  clearQuestion = "CLEAR_QUESTION",
  displayFollowUp = "DISPLAY_FOLLOWUP",
  endGame = "END_GAME",
  addPlayer = "ADD_PLAYER",
  removePlayer = "REMOVE_PLAYER",
  disconnected = "DISCONNECTED",
  resetGame = "RESET_GAME"
}

import { PlayActions } from 'plearnit-connector';

PlayActions.startGame
Pellegrino answered 28/10, 2024 at 10:11 Comment(1)
Please add a description to your answer that explains how this code works to answer the question.Staub

© 2022 - 2025 — McMap. All rights reserved.