microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.05k stars 12.49k forks source link

Support function reload in class #48582

Closed keyiis closed 2 years ago

keyiis commented 2 years ago

If there are multiple methods with the same name but different parameters in a class, their comments are also different, which cannot be implemented in typescript at present. Example:

import { EventEmitter } from 'events';
class ClassA extends EventEmitter{
    constructor(){
        super();
        this.emit("a","abc");
        this.emit("b","abc",123);
    }
    //I added two "on" method so that VSCODE can prompt comments and parameter types when using it.but typescript does not allow functions with the same name.
    /**
      *  comments about event "a"
      */
    on(eventName: "a", listener: (arg:string) => void){
         return super.on(eventName,listener);
    }

     /**
      *  comments about event "b"
      */
    on(eventName: "b", listener: (arg1:string,arg2:number) => void){
         return super.on(eventName,listener);
    }
}

let insA = new ClassA();
// I hope vscode can prompt comments and parameter types when using it
insA.on("a",(arg:any)=>{
       // "arg" type cannot be inferred
});
fatcerberus commented 2 years ago

TS does support overloads, but they have to share a single implementation: https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads

What you wrote is not valid TS.

keyiis commented 2 years ago

@fatcerberus Thanks reply! I have seen this document, But it doesn't seem to meet my needs, the ClassA extend EventEmitter, In fact, I don't need to implement the on method, But I hope there will be specific hints for specific events,so i have to be able to define these special tips somewhere.

fatcerberus commented 2 years ago

@keyiis It does meet your needs: https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgUQG4FMB2NkmDGdKOAXzgDMoIQ4BydDbAZ1oG4AodgYwBsBDJkzgBhfoICCcdAA9CmACZC0WHHgJEE7ONrhcImJjCgBXLjGgAKAJSadduE2Ngi1jvZ0wAFsCYA6dGoWAER8QQA0IQBGXEFWbu5wXj7+gUGR4VExYQCMAEwAzHFaOiScdgD0AFSVxXaV2nogICpCfJEQxvAMKnAhQbU6leUD+hbd2AByfM0AXL2hYXA8PnJEcxZ8UADmM4ZQwJhbVnAAvAB8cKgQwPJWc0lMHANwVTXu9brUzcxwbR1djHgaX672GdlG4xgU1mvXSi2WhiwazgG222V2RgOWzCmy2uRmmGMIEiRGO50u11u928jzKOnK5USNLgPiZ6BZ4B4ARUfBgwH0cAg5Do+loi3wtCEmAg8FQPmAkS5iQguj4PC5UCYI0wY0B0PQcz6cAAPrDwksVkioOtcei9ljFrj8YTiaTThcrjc7kzWbYEnAoOgYMYoJgHE4iL4IXrpugwgjVlAinZSqV2Fz4AcmJITnBMOgAO4iMTZ1ycBlwTwQDDEatEEWYWjKhzodkAKQAygARCBcCjQKSAuDidhZ8RRnV9MKoo7nTQV-3aAB6AH52CQikA

keyiis commented 2 years ago

@fatcerberus Thank you very much for your help!