objection.js icon indicating copy to clipboard operation
objection.js copied to clipboard

Question: PromiseLike instead of Promise?

Open hardeep opened this issue 9 years ago • 4 comments

I'm using the bluebird library, it seems that the type definitions specify Promise instead of PromiseLike.

Any reason for using Promise vs PromiseLike? I'd be willing to submit a PR to change it.

hardeep avatar Nov 29 '17 23:11 hardeep

@mceachen Could you take a look at this?

koskimas avatar Dec 01 '17 21:12 koskimas

@hardeep Promise and PromiseLike are mostly ducktype compatible, but Promise neither extends nor implements PromiseLike in the TypeScript 2.6.2 typings. You'd have to change every reference to Promise to be Promise<T> | PromiseLike<T>, and callers would have to either cast (which is unsafe) or instanceof guard those calls if they had consumers that only accepted Promises.

About a year ago I'd tried to use PromiseLike when Promise wasn't necessary in my own projects, but it caused so much signature bloat I ended up only using the Promise signature internally.

If there's a clever way to do this that I'm missing, I'm all ears!

mceachen avatar Dec 01 '17 22:12 mceachen

Will reopen if @hardeep or others offer workarounds.

mceachen avatar Jan 03 '18 01:01 mceachen

You'd have to change every reference to Promise to be Promise | PromiseLike

Has something changed? This compiles on 2.8:

function foo<T>(bar: PromiseLike<T>): void {
    bar.then(alert)
}

foo(new Promise((resolve, reject) => resolve(1)));

Of course, consumers that only accept Promise are another story.

Aankhen avatar Jun 09 '18 21:06 Aankhen