简体   繁体   English

无法使用使用不同参数的覆盖方法扩展Typescript类?

[英]Extending Typescript Class with overriding method using different parameter not possible?

I'm writing a little MVC app in Typescript but i have prolem. 我在Typescript中写了一个小MVC应用程序,但我遇到了麻烦。 If i extend my BaseController and override the ajaxMethod what needs diferent parameters my transpiler makes an error. 如果我扩展BaseController并覆盖ajaxMethod,则需要哪些不同的参数,我的编译器会出错。 hope someone can help me. 希望可以有人帮帮我。

here's my code: 这是我的代码:

interface i_Controller {
    ajaxMethod;
    ajaxSuccessListener;
    ajaxErrorListener;
}

class BaseController implements i_Controller {
    protected baseProperty: boolean;

    constructor() {
        this.baseProperty = true;
    }

    public ajaxMethod() {
        $.when(
            $.ajax({})
        ).then(
            this.ajaxSuccessListener,
            this.ajaxErrorListener
        )
    }

    public ajaxSuccessListener(data, status, jqXHR) {
        console.log('ajax success');
        console.log(data);
    };

    public ajaxErrorListener(jqXHR, status, error) {
        console.error('ajax error');
        console.error(status);
    };
}


class Page_1_Controller extends BaseController {
    private localProperty: number;

    constructor(input) {
        super();
        this.localProperty = input;
    }

    public ajaxMethod(someProperty) {
        /*
        /* Error:(39, 7) TS2415:Class 'Page_1_Controller' incorrectly
        /* extends base class 'BaseController'.
        /* Types of property 'ajaxMethod' are incompatible.
        /* Type '(someProperty: any) => void' is not assignable to 
        /* type '() => void'.
        */
        $.when(
            $.ajax({
                data: {properties: someProperty}
            }),
            $.ajax({})
        ).then(
            this.ajaxSuccessListener,
            this.ajaxErrorListener
        )
    }

    public ajaxSuccessListener(responseAjaxRequest_1, responseAjaxRequest_2) {
        console.log('ajax success');
        let data_1 = responseAjaxRequest_1[0];
        let data_2 = responseAjaxRequest_2[0];
        console.log(data_1);
        console.log(data_2);
    }
}

class MyApp {
    private controller: i_Controller;

    constructor() {
        this.controller = new Page_1_Controller();
        /*
        /* Error:(72, 27) TS2346:Supplied parameters do not match any
        /* signature of call target.
        */
        this.controller.ajaxMethod();
    }
}

at the moment i don't know where i am false on extending my classes. 目前,我不知道我在扩大班级上的错误之处。 constructor can be overwritten without any problems, and the listeners too. 构造函数可以被覆盖而没有任何问题,侦听器也可以被覆盖。 why not the ajaxMethod? 为什么不使用ajaxMethod?

Just as the error message said, the signature of the two ajaxMethod() are incompatible. 就像错误消息所说的那样,两个ajaxMethod()的签名是不兼容的。

When Page_1_Controller extends BaseController , it gets the type of ajaxMethod() as () => void . Page_1_Controller扩展BaseController ,它将获得ajaxMethod()的类型为() => void This means that if your instance of Page_1_Controller is down-casted to BaseController , it should work with that signature. 这意味着,如果您的Page_1_Controller实例向下转换为BaseController ,则它应使用该签名。

Consider this: 考虑一下:

function foo(c: BaseController) {
  c.ajaxMethod()
}
const page1 = new Page_1_Controller()
foo(page1)

Your code won't be able to handle this. 您的代码将无法处理此问题。 That's why the compiler complains and helps you catching this error during compile time. 这就是为什么编译器会抱怨并帮助您在编译期间捕获此错误的原因。

To fix this, you should handle this case, such as: 要解决此问题,您应该处理这种情况,例如:

class Page_1_Controller extends BaseController {
  ajaxMethod(someProperty?) {
    if (someProperty) {
      ...
    }
    else {
      super.ajaxMethod()
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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