Skip to content

TypeScript types #1

@mikaello

Description

@mikaello

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions