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.

Exporting TypeScript types

See original GitHub issue

Whilst migrating the main helmet package over to TypeScript, I discovered an issue to do with exporting types:

Property 'dnsPrefetchControl' of exported interface has or is using name 'DnsPrefetchControlOptions' from private module '"./node_modules/dns-prefetch-control/dist/index"'.

In this example, I believe the DnsPrefetchControlOptions type needs to be exported in the main module definition. However, because we are using CJS exports, we cannot simply prefix the type definition with export (as there can only be a single export). Instead, the suggested way is to use namespaces:

/* eslint-disable @typescript-eslint/no-namespace */
namespace dnsPrefetchControl {
  export interface DnsPrefetchControlOptions {
    /* ... */
  }
}

// eslint-disable-next-line no-redeclare
function dnsPrefetchControl (options: dnsPrefetchControl.DnsPrefetchControlOptions = {}) {
  /* ... */
}

In the interest of preserving CJS exports, does this seem like a reasonable way for now? In the future, we can make a breaking change to use ES modules everywhere which would make things a lot nicer, but this change would at least make it possible to migrate this module to TypeScript. 👍

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
EvanHahncommented, Aug 2, 2020

TypeScript types were added to Helmet in version 4, which you can install with npm install helmet@4. Closing this issue because I believe this is resolved, but feel free to comment or open a new issue.

1reaction
EvanHahncommented, Sep 14, 2019

Ah, okay, I think that all makes sense to me now.

Given that…

  1. helmet@4 is going to support all the way down to Node 8
  2. Node 8 doesn’t support ES modules natively (without a flag, at least)
  3. I want to prioritize native, no-transpilation setups over setups that require a transpiler (TypeScript or otherwise)

I think we should use export = like we do now. If that means we have to use deprecated TypeScript namespaces, that’s okay with me. It’s not ideal, but based on the givens above (which we could certainly debate), I think that’s the only option.

Does that sound right?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Modules - TypeScript
The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or...
Read more >
How to export a Type in TypeScript | bobbyhadz
Use a named export to export a type in TypeScript, e.g. export type Person = {} . The exported type can be imported...
Read more >
What is `export type` in Typescript? - javascript - Stack Overflow
This is a type alias - it's used to give another name to a type. In your example, feline will be the type...
Read more >
export type * from 'somewhere' #48508 - GitHub
Suggestion Search Terms typescript export type star ✓ Viability Checklist My suggestion meets these guidelines: This wouldn't be a ...
Read more >
Leveraging Type-Only imports and exports with TypeScript 3.8
TypeScript Type -Only imports/exports are a useful addition to the language, allowing us to have more fine-grained control over what TypeScript does for...
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