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?
if (process.env.NODE_ENV === "production") {
configureStore = require("./configureStore.prod").default;
} else {
configureStore = require("./configureStore.dev").default;
}
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