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.

Inconvenient types import / export

See original GitHub issue

First and foremost: Great that you switched from Flow to TypeScript! Although, when switching from @types/cosmiconfig to the types which are now provided by this package, I experienced the following issues:


No direct import for CosmiconfigResult

Currently when I want to declare a variable which will get the result from cosmiconfig.search(), I need to do it like this:

import {cosmiconfig} from 'cosmiconfig';
import {CosmiconfigResult} from 'cosmiconfig/dist/types';

let result: CosmiconfigResult;

But I’d like to import it conveniently like this:

import {cosmiconfig, CosmiconfigResult} from 'cosmiconfig';

let result: CosmiconfigResult;

Unfortunately this is not possible since CosmiconfigResult is not re-exported from the types dir. I tried fixing it but since isolatedModules: true is set in tsconfig.json, re-exporting is disallowed:

src/index.ts:137:56 - error TS1205: Cannot re-export a type when the '--isolatedModules' flag is provided.

137 export { cosmiconfig, cosmiconfigSync, defaultLoaders, CosmiconfigResult };

No clear return type of cosmiconfig()

I have a class which adds cosmiconfig as a property and I have to define its type. Since cosmiconfig() returns an object (and is especially excluded from needing a convenient return type), I need to declare it like this:

import {cosmiconfig} from 'cosmiconfig';

class MyClass {
  private readonly cosmiconfig: ReturnType<typeof cosmiconfig>;

  constructor() {
    this.cosmiconfig = cosmiconfig('myconfig');
  }
}

I’d rather like to be able to declare it like this:

import {cosmiconfig, Cosmiconfig} from 'cosmiconfig';

class MyClass {
  private readonly cosmiconfig: Cosmiconfig;

  constructor() {
    this.cosmiconfig = cosmiconfig('myconfig');
  }
}

Where Cosmiconfig is an interface which describes the return value of cosmiconfig().

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
fffloriancommented, Nov 6, 2019

I personally think using ReturnType is the preferred way for a consumer of a library to get a the return type of a function

I disagree. ReturnType is a Utility Type which IMO is a good fallback if you don’t have any interface on hand. The preferred way should be a clear interface. But that’s just my opinion. 🙂

0reactions
davidtheclarkcommented, Jul 13, 2020

Closing as stale.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exporting and Importing - Meaning, Advantages and ...
Exporting goods and services refer to sending them from the home country to a foreign country. Similarly, Importing goods and services means ...
Read more >
Advantage & Disadvantages Of Export Import Business
Disadvantages of Exporting: · 1 Foreign Exchange Rate: Profit Margins Might get affected due to fluctuations in foreign exchange rate. · 2 Economic...
Read more >
Advantages and disadvantages of exporting - NIBusinessInfo
Exporting outside Northern Ireland can change your business. Like any fundamental change to the way you trade, there are risks as well as...
Read more >
What Are Advantages And Disadvantages Of Importing
Import and export are two types of major business activities by which you can easily become a part of the international marketplace. When...
Read more >
Advantages and Disadvantages of Importing And Exporting
Import and export are the two basic and primary ways of conducting the business (Dunning, 2007). Whenever a company engages into the international...
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