简体   繁体   English

具有TypeScript的ExtJS,未捕获错误

[英]ExtJS with TypeScript, error not caught

I'm trying out TypeScript with Sencha ExtJS: 我正在尝试使用Sencha ExtJS的TypeScript:

/// <reference path="../ExtJS.d.ts" />

Ext.define("CompanyGridPanel", <Ext.grid.IPanel>{
    extend: "Ext.grid.Panel",

    config: {
        companyStore: null
    },

    initComponent: function () {
        this.myFunction(10, 52);

        return this.callParent(arguments);
    },

    myFunction : (a:number, b:Ext.IPanel, ...args) => {

    }
});

Function MyFunction accepts object of type Ext.IPanel as second parameter but is called with number instead. 函数MyFunction接受Ext.IPanel类型的对象作为第二个参数,但用数字代替。 TS compiler does not catch this error, but WebStorm IDE does catch it correctly. TS编译器未捕获此错误,但WebStorm IDE确实正确捕获了此错误。

Why TS don't catch it and how can it be fixed? 为什么TS找不到它,如何解决?

Using ExtJS 4.2 and TS 0.9.5.0. 使用ExtJS 4.2和TS 0.9.5.0。

The TypeScript compiler will attempt to infer the type of the object in question (b:Ext.IPanel) based on the function call. TypeScript编译器将尝试基于函数调用来推断所讨论对象的类型(b:Ext.IPanel)。

Consider the following code: TypeScript will infer that the variable foo is a string, as the return value of the getValue() function is a string, therefore the var foo becomes a string. 考虑以下代码:TypeScript将推断变量foo是一个字符串,因为getValue()函数的返回值是一个字符串,因此var foo成为一个字符串。

In your sample above, this.myFunction(10,52) succeeds, because the compiler is able to match Ext.IPanel to a number, thereby inferring b as a number. 在上面的示例中,this.myFunction(10,52)成功,因为编译器能够将Ext.IPanel与数字匹配,从而将b推断为数字。

class Test {
   constructor() {
      var foo = getValue();
   }
   getValue() {
      return "bar";
   }
}

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

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