首页 > 解决方案 > Flow restrict to extend a class by assigment methods from callback

问题描述

StackOverflow community! The $Subtype<> utility allows to do a lot of weird stuff flowtyping. Now I need to escape old functionality using the $Super utility and Generics. Literally flow spec is very brief, I couldn't find anything about how to extend an instance of a class with new methods. Please, help me to FlowFix .

I've tried a lot of tricks, but, look, I can't change a conduct aka usage of this function.

/* @flow */

class MainClass<C> {
    constructor(
        callback: (
            resolve: (result?: any) => void,
            reject: (error?: any) => void
        ) => mixed
    ): void {
        // Contructor things
    }
}

type ExtendedClass<D> = MainClass<D> & {
    resolve(D): void;
    reject(Error): void;
}

export function extended<D>(): ExtendedClass<D> {
    let resolve;
    let reject;
    const main: MainClass<D> = new MainClass((res, rej) => {
        resolve = res;
        reject = rej;
    });

    main.resolve = resolve;
    main.reject = reject;

    return main;
}

Try this example

main.resolve = resolve;
          ^ Cannot assign `resolve` to `main.resolve` because property `resolve` is missing in `MainClass` [1].

    References:

    22:     const main: MainClass<D> = new MainClass((res, rej) => {
                     ^ [1]

标签: javascriptflowtypeflow-typed

解决方案


推荐阅读