Typescript specialized overloads defined on a subclass or interface

后端 未结 3 956
南笙
南笙 2020-12-19 11:23

Is there a way to make the following work without having to define an implementation in the subclass which simply calls the superclass or unnecessarily repeats the non-speci

3条回答
  •  猫巷女王i
    2020-12-19 12:08

    Function overloads only get combined if they are call signatures on an object type. The easiest fix (for the interface case) is to separate out the function type into its own interface and extend that:

    interface EmitterEvent {
        (name: 'one', handler: (value: number) => void): void;
        (name: string, handler: (...args: any[]) => void): void;
    }
    
    interface SubclassEmitterEvent extends EmitterEvent {
        (name: 'two', handler: (value: string) => void): void;
    }
    
    interface IEmitter {
        on: EmitterEvent;
    }
    
    interface ISubclass extends IEmitter {
        on: SubclassEmitterEvent;
    }
    
    var x: ISubclass;
    x.on('one', n => n.toFixed()); // n: number
    x.on('two', s => s.substr(0)); // s: string
    var y: IEmitter;
    y.on('two', a => a); // a: any
    

    The equivalent version in the class case takes some work (assuming you care about the function going on the prototype -- if not, just use a function expression as an initializer for on instead):

    class Emitter {
        on: EmitterEvent;
    }
    module Emitter {
        Emitter.prototype.on = function(name: string, handler: any) {
            // Code here
        }
    }
    

提交回复
热议问题