egoist / egoist/computer-name

TypeScript types

Open
#1 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
18
Forks
4
PR merge metrics
No merged PRs in 30d

Description

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:

```js
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`):

```typescript
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'`:

```typescript
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.