7

I have the following JS code from a tutorial:

if (process.env.NODE_ENV === 'production') {
  module.exports = require('./configureStore.prod');
} else {
  module.exports = require('./configureStore.dev');
}

The configureStore.*.ts files both have a default export:

export default function configureStore(initialState?: State) {
    // ...
}

I want to translate the conditional export in the former code snippet into TypeScript. If I just leave the code as is, I get a compile-time error:

error TS2306: File 'configureStore.ts' is not a module.

After some trial and error I could get the following to compile:

import {Store} from "redux";
import {State} from "../reducers";

let configureStore: (state?: State) => Store<State>;

if (process.env.NODE_ENV === "production") {
  configureStore = require("./configureStore.prod");
} else {
  configureStore = require("./configureStore.dev");
}

export default configureStore;

I tried using it like this:

import configureStore from "./store/configureStore";

const store = configureStore();

But I got this error message at runtime:

TypeError: configureStore_1.default is not a function

How can I successfully translate the original code into TypeScript?

1 Answer 1

4
if (process.env.NODE_ENV === "production") {
  configureStore = require("./configureStore.prod").default;
} else {
  configureStore = require("./configureStore.dev").default;
}
Sign up to request clarification or add additional context in comments.

Comments

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.