sindresorhus / ky-universal

Use Ky in both Node.js and browsers
https://github.com/sindresorhus/ky
MIT License
670 stars 20 forks source link

Import in typescript #16

Closed ishpagin closed 4 years ago

ishpagin commented 4 years ago

Hi all,

Don't working default import in ts, like import ky from 'ky-universal' is undefiend but:

const ky = require('ky-universal')

is working

sholladay commented 4 years ago

I believe this is to be expected. Unlike ky, which is a pure ES module, ky-universal is a CommonJS module. I think Sindre did it this way because CommonJS is still a little easier to consume in the Node.js ecosystem.

By the way, it is probably better to write:

import ky = require('ky-universal');

My understanding is that const ... require() returns the module with type any, whereas import ... require() returns the correct type.

ishpagin commented 4 years ago

Hi @sholladay,

Thank you for quick response.

import ky = require('ky-universal')

This way shows me only "ky.default.post" path in hints in vscode and works incorrectly because ky-universal exported object that doesn't have the "default" field and app crashed in runtime. If I don't use the "default" field it shows error with compiling and linting in TS.

My solution is add the definition of "ky-universal" module

// ky.d.ts
declare module 'ky-universal' {
  import ky from 'ky';
  export = ky
}

Can we add this definition to fix IDE hits and typescript compiler errors?

sindresorhus commented 4 years ago

I think this is a problem with our type definition. I think it should be changed to:

import ky from 'ky';

export = ky;

which is the correct CommonJS syntax.

ishpagin commented 4 years ago

@sindresorhus

Yes, it can be the solution.

Should I create PR or would you be able to delegate this change to someone?

sholladay commented 4 years ago

I have submitted PR #17 to fix that issue @ishpartko. After it is merged and released, your workaround should no longer be necessary.