question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript and export HelmetOptions

See original GitHub issue

Hi,

We have a function that looks similar to:

/**
 * Get a list of default Helmet options we use for e.g. our
 * http and websocket (socket.io) routes.
 *
 * @returns {HelmetOptions} The options to use with Helmet
 */
function getHelmetOptions() {
  return {
    hsts: {
      maxAge: 31536000,
    },
    expectCt: false,
  };
}

The reason is we use these options both for basic Express apps but also for socket.io routes. But there is no way to import the HelmetOptions interface like import {HelmetOptions} from "helmet";

Could you, in addition to the default export, also export the named export HelmetOptions for usage scenarios like above?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
EvanHahncommented, Jan 19, 2022

I think only attempt 3 should succeed, so things are working as expected. (I think!)

Attempt 1 is trying to import HelmetOptions which is a TypeScript type. You can’t import TypeScript types from plain JavaScript, so this shouldn’t work.

Attempt 2 imports Helmet’s default export, not the Helmet namespace. HelmetOptions is not part of the default export, so this shouldn’t work either. It seems that Express works differently.

Attempt 3 imports Helmet as a namespace. HelmetOptions is part of that namespace, so it should work.

Hope this helps.

0reactions
thernstigcommented, Jan 19, 2022

@EvanHahn I tested a few things with this and have some findings. My knowledge of TypeScript is not good enough to deduct if there is something that can be done or not, but wanted to share. Note we are not using TypeScript directly in our code, but more the TypeScript in JSDoc (via a jsconfig.json) which is how some editors support TypeScript in JavaScript projects, such as VS Code.

Attempt 1 (failed)

import { HelmetOptions } from 'helmet';

/**
 * Get a list of default Helmet options
 *
 * @returns {HelmetOptions} The options to use with Helmet
 */
# This fails during running node with the above code
file:///home/userA/code/projectA/server/util/security.js:1
import { HelmetOptions } from 'helmet';
         ^^^^^^^^^^^^^
SyntaxError: The requested module 'helmet' does not provide an export named 'HelmetOptions'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:181:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Attempt 2 (failed)

import helmet from 'helmet';

/**
 * Get a list of default Helmet options
 *
 * @returns {helmet.HelmetOptions} The options to use with Helmet
 */

Attempt 3 (passed)

import * as helmet from 'helmet';

/**
 * Get a list of default Helmet options
 *
 * @returns {helmet.HelmetOptions} The options to use with Helmet
 */

Alternate example with the express module (passes)

import express from 'express';

/**
 * Some function
 *
 * @returns {express.Request} Express req object
 */

As you can see for some reason at least the Express example works, which mimics attempt 2.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exporting TypeScript types · Issue #209 · helmetjs/helmet
Whilst migrating the main helmet package over to TypeScript, I discovered an issue to do with exporting types: Property 'dnsPrefetchControl' ...
Read more >
TypeScript: Documentation - Modules
Exporting a declaration. Any declaration (such as a variable, function, class, type alias, or interface) can be exported by adding the export keyword ......
Read more >
Upgrading Helmet to v4.5.0 and needing equivalent type to ...
Rework your code a bit. You could update the Options interface to take all of Helmet's options: import helmet from 'helmet'; export ......
Read more >
Modules & CJS interop - Learn TypeScript w/ Mike North
ES Module imports and exports. First, let's get the conventional stuff out of the way: TypeScript does exactly what you're used to seeing...
Read more >
Announcing TypeScript 4.7 Beta
Today we are excited to announce the beta release of TypeScript 4.7! ... import / export statements (and top-level await in nodenext )...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found