Skip to content

better type coercion : instead of cannot invoke an expression whose type lacks a call signature #20877

Closed
@SalathielGenese

Description

@SalathielGenese

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 :

  1. const EVENT_EMMITTERS: WeakMap<EventEmitter<any>, Map<string, EventHandlerLike<any>[]>> = new WeakMap();
  2. The this context is instance of EventEmitter

Expected behavior:
I had expected these to be inferred after the.or second call:

  1. parameter p as number | string
  2. 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...'

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions