Closed
Description
TypeScript Version: 2.6.2
{
"compilerOptions": {
"strict": true,
Code
export interface HopeLike<T, S = void>
{
<U>(callabale: (value: T) => U, asHoped?: (value: U) => boolean): HopeLike<U, T>;
or<U>(callable: (parent: S, value: T) => U): this | HopeLike<U, T>;
get(): T | void;
}
let h: HopeLike<number> = <any>void 0;
h(() => 1).or(() => '').or(p => ({})); // here is the issue, from the `.or(...`
Note that this code works well if .or
call is not called twice!
A concrete usecase
const EVENT: Event<typeof source> = new Event(event, source);
const HANDLERS = hope(EVENT_EMMITTERS.get(this)!)
(container => container.get(event)!)
// Since it is not sure this key is mapped, let's ensure, just in case.
// This' achieved by calling `.or(callback)` which will run its callback only if the `event` key is mapped
.or(container => container.set(event, []).get(event)!)
.get(); // were finally fetch the real result, which may be `void` as well
if (HANDLERS) for (let handler of HANDLERS)
if (void handler.call(EVENT, EVENT, ...data) || EVENT.isPropagationStopped)
break;
Where :
const EVENT_EMMITTERS: WeakMap<EventEmitter<any>, Map<string, EventHandlerLike<any>[]>> = new WeakMap();
- The
this
context is instance ofEventEmitter
Expected behavior:
I had expected these to be inferred after the.or
second call:
- parameter
p
asnumber | string
- the return value to be:
HopeLike<number, number> | HopeLike<U, number> | HopeLike<string, number> | HopeLike<U, string>
Actual behavior:
The error says cannot invoke an expression whose type lacks a call signature. Type '(<U>(callable: (parent: number, value: number) => U): HopeLike<number, number> | HopeLike<U, nu...'