Open lev-kuznetsov opened 6 years ago
Hi @lev-kuznetsov. Thanks for the issue!
I'm planning on rewriting this library in TypeScript, so I will revisit this then.
Still can't import this library with TypeScript
A short-term alternative if you need one is to use the following syntax.
const interval = require('interval-promise')
@andyfleming actually I rewrote the type file like so:
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;
function interval(func: func,intervalLength: intervalLength, options?:IIntervalPromiseOptions): Promise<void>
export = interval;
}
You really should consider updating the type file. I can make a quick PR if you're willing to merge it
Using this just fine in TS 3.4 when giving the "esModuleInterop" compiler flag... and to be frank you're really shooting yourself in the foot in this day and age if you don't give that option.
I don't know if this is continuation of #6
Documented way of importing in ts is
Which expects the actual interval function to be on
module.exports.default
in the javascript world except it's onmodule.exports
. Documented way of importing allows to compile since it's inline with the type file but doesn't work at runtime sincemodule.exports.default
is undefined.import * as interval from 'interval-promise'
expectedly won't compile the function call unless you then cast:Which obviously just bypasses the type file. What I think you want to put into the type file is:
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.tsMy tsconfig:
I can create a PR to this effect. Also might I suggest to allow
func
to returnPromise<any>
instead ofPromise<void>
- you don't really care what it returns as long as it's a promise, right?