Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript types #1

Open
mikaello opened this issue Dec 12, 2019 · 2 comments
Open

TypeScript types #1

mikaello opened this issue Dec 12, 2019 · 2 comments

Comments

@mikaello
Copy link

I tried to create TypeScript types for this module, but couldn't figure out how to do it in combination with importing it as a module:

import * as computerName from 'computer-name'

I need to use the * as otherwise I get an error at runtime telling me tab3.tsx? [sm]:16 Uncaught TypeError: computer_name_1.default is not a function.

I can't manage to type this import and get computerName to be a function returning a string. Has someone done this?

Here is what I tried so far (content of my src/types/computer-name.d.ts):

export = computerName;
declare function computerName(): string;

It is not recognized when I import computer-name, it gives the standard warning about no types. When I add the suggested declare module 'computer-name':

declare module 'computer-name' {
  export = computerName;
  function computerName(): string;
}

TS now gives this warning: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.. esModuleInterop is on, so it seems to be the star import.

When I use const computerName = require('computer-name'), computerName will be typed as any, so that works for now, but it would be nice if I could get it correct.

@mikaello
Copy link
Author

I actually made it work now by changing the import to:

import computerName = require('computer-name');

And the computer-name.d.ts file contains:

declare module 'computer-name' {
  export = computerName;
  function computerName(): string;
}

@lockieluke
Copy link

#4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants