1

I'm exporting a function from a controller file:

import adminSaveSnippet from './admin/adminSaveSnippet';
exports.saveSnippet = adminSaveSnippet;

I am importing the function into my routing file:

import * as express from 'express';
const adminRouter = express.Router();
import * as admin from '../controllers/adminControllers';

adminRouter.put('/save/snippet', admin['saveSnippet']);

export default adminRouter;

I can access the saveSnippet function in admin via admin['saveSnippet'] but when I use dot notation admin.saveSnippet I get the following error:

src/routes/admin.ts:5:40 - error TS2339: Property 'saveSnippet' does not exist on type 'typeof import(".../src/controllers/adminControllers")'.

I am not really sure what is going on. I am not using any irregular identifiers and I have tried several different ways of exporting the function (e.g., import { saveSnippet } from '../controllers/adminControllers') but still get the same error.

For completeness here is my adminSaveSnippet file:

export default function adminSaveSnippet (_req, res) {

  console.log('here');
  res.send('saved').status(200);
}
ghybs
  • 47,565
  • 6
  • 74
  • 99
badinvestor
  • 67
  • 1
  • 10

1 Answers1

0

This is most probably just a TypeScript syntax issue.

In your adminControllers.ts file, try the export syntax, like you have done in your adminSaveSnippet file, instead of the CommonJS exports:

export {
  adminSaveSnippet as saveSnippet
}
ghybs
  • 47,565
  • 6
  • 74
  • 99