简体   繁体   中英

How to implement generic method overloading with different type parameters in Typescript?

For a library function I need to use method overloading with different type parameters, but I cannot figure out how to do the implementation. My attempt probably uses a wrong implementation signature:

namespace java {
  export namespace lang {
    export class Object {}

    export interface Comparable<T> {
      compareTo: (other: T) => boolean;
    }
  }

  export namespace util {
    export interface Collection<T> {
      [Symbol.iterator](): IterableIterator<T>;
    }
    export interface Comparator<T> {
      compare: (a: T, b: T) => number;
    }
  }
}

class Collections {
    public static max<T1 extends java.lang.Object & java.lang.Comparable<T1>>(
        coll: java.util.Collection<T1>): T1 | null;
    public static max<T2>(coll: java.util.Collection<T2>, comp: java.util.Comparator<T2>): T2 | null;
    public static max<T>(coll: java.util.Collection<T>, comp?: java.util.Comparator<T>): T | null {
        let result: T | null = null;

        for (const current of coll) {
            if (result === null) {
                result = current;
                continue;
            }

            const comparison = comp ? comp.compare(result, current) : result?.compareTo(current);
            if (comparison < 0) {
                result = current;
            }
        }

        return result;
    }
}

Here's also a playground example . What would be the correct approach in this situation?

Here's a possible solution, which I found when I stopped trying to use an alternative of the two type parameters:

    public static max<T1 extends java.lang.Object & java.lang.Comparable<T1>>(
        coll: java.util.Collection<T1>): T1 | null;
    public static max<T2>(coll: java.util.Collection<T2>, comp: java.util.Comparator<T2>): T2 | null;
    public static max<T1 extends java.lang.Object & java.lang.Comparable<T1>, T2>(
        coll: java.util.Collection<T1> | java.util.Collection<T2>, comp?: java.util.Comparator<T2>): T1 | T2 | null {
        if (comp) {
            let result: T2 | null = null;

            for (const current of coll as java.util.Collection<T2>) {
                if (result === null) {
                    result = current;
                    continue;
                }

                const comparison = comp.compare(result, current);
                if (comparison < 0) {
                    result = current;
                }
            }

            return result;
        } else {
            let result: T1 | null = null;

            for (const current of coll as java.util.Collection<T1>) {
                if (result === null) {
                    result = current;
                    continue;
                }

                const comparison = result.compareTo(current);
                if (comparison < 0) {
                    result = current;
                }
            }

            return result;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM