Skip to content

Can't import in typescript #8

@lev-kuznetsov

Description

@lev-kuznetsov

I don't know if this is continuation of #6

Documented way of importing in ts is

import interval from 'interval-promise'

Which expects the actual interval function to be on module.exports.default in the javascript world except it's on module.exports. Documented way of importing allows to compile since it's inline with the type file but doesn't work at runtime since module.exports.default is undefined. import * as interval from 'interval-promise' expectedly won't compile the function call unless you then cast:

import * as interval from 'interval-promise'

(<any>interval)(async () => console.log('hello world'), 5000)

Which obviously just bypasses the type file. What I think you want to put into the type file is:

declare module 'interval-promise' {
  interface IIntervalPromiseOptions {
    iterations?: number;
    stopOnError?: boolean;
  }

  type stop = () => void;
  type func = (iterationNumber: number, stop: stop) => Promise<void>;
  type intervalLengthFn = (iterationNumber: number) => void;
  type intervalLength = number | intervalLengthFn;
}

declare function interval(
  func: func,
  intervalLength: intervalLength, options?:
  IIntervalPromiseOptions): Promise<void>

export = interval

That way you'll be able to import via import * as interval from 'interval-promise' which is what you have to do because that's how it's documented for javascript. And this is inline with how the DefinitelyTyped guys do it here: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/koa-compose/index.d.ts

My tsconfig:

{
  "version": "2.9.2",
  "compilerOptions": {
    "lib": ["es5", "es6", "dom"],
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "outDir": "target",
    "sourceMap": true
  },
  "exclude": [
    "node_modules",
    "target"
  ]
}

I can create a PR to this effect. Also might I suggest to allow func to return Promise<any> instead of Promise<void> - you don't really care what it returns as long as it's a promise, right?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions