简体   繁体   English

带有VS2013 Update 2 CTP的Typescript 1.0:我们代码中的许多重大更改

[英]Typescript 1.0 with VS2013 Update 2 CTP: lots of breaking changes in our code

Today tried updating VS2013 to the Update 2 CTP release. 今天尝试将VS2013更新为Update 2 CTP版本。 All works fine but the TypeScript compiler has become too strict with our code. 一切正常,但是TypeScript编译器对我们的代码过于严格。 All code that was written with the 0.9.1.1 version is broken after the Update. 在更新之后,使用0.9.1.1版本编写的所有代码都会损坏。

We have for instance code that extends the Kendo DataSource. 例如,我们有扩展Kendo DataSource的代码。 In the DataSource a 'transport' property is present that has a 'parameterMap' property that accepts a function that is defined as follows: 在数据源中,存在一个“传输”属性,该属性具有一个“ parameterMap”属性,该属性接受定义如下的函数:

interface DataSourceTransport {
    parameterMap?(data: DataSourceTransportParameterMapData, type: string): any;
}

interface DataSourceTransportParameterMapData {
    aggregate?: DataSourceParameterMapDataAggregate[];
    group?: DataSourceParameterMapDataGroup[];
    filter?: DataSourceParameterMapDataFilter;
    models?: Model[];
    page?: number;
    pageSize?: number;
    skip?: number;
    sort?: DataSourceParameterMapDataSort[];
    take?: number;
}

As you can see the parameterMap parameter accepts a function accepting a 'DataSourceTransportParameterMapData'. 如您所见,parameterMap参数接受一个接受“ DataSourceTransportParameterMapData”的函数。

Now in our code we extend this parameter with some extra properties like so: 现在,在我们的代码中,我们使用一些额外的属性扩展了此参数,如下所示:

parameterMap: function (data: any, operation: string) {
if (operation === "create") {
    data.TypeName = 'ActiviteitEntity';
}

if (operation === "update" || operation === 'destroy') {
    data.Xml = contractors.htmlEncode(data.Xml);
}               }

This construct was excepted fine by the 0.9.1.1 compiler but the 1.0 compiler tells me there is not 'TypeName' and 'Xml' property on the 'DataSourceTransportParameterMapData' which is correct. 0.9.1.1编译器可以很好地使用此构造,但是1.0编译器告诉我'DataSourceTransportParameterMapData'上没有'TypeName'和'Xml'属性,这是正确的。 But we defined the 'data' parameter as an 'any' type. 但是我们将“数据”参数定义为“任意”类型。

So although we defined it as 'any' the compiler still refuses to add the new property (which btw would work fine in JavaScript). 因此,尽管我们将其定义为“ any”,但编译器仍然拒绝添加新属性(顺便说一句在JavaScript中可以正常工作)。

This is just one of the problems I'm facing now but there are lots more which I haven't even started analizing yet. 这只是我现在面临的问题之一,但是还有很多我还没有开始分析的问题。

I've uninstalled the Update 2 CTP for the moment but there will come a time that will have to switch to TypeScript 1.0. 我暂时已经卸载了Update 2 CTP,但是会有一段时间必须切换到TypeScript 1.0。 As I now estimate there is not a single piece of code that is not affected by the 1.0 version which is catastrophic. 正如我现在估计的那样,没有哪段代码不受灾难性的1.0版本的影响。

I can bet the following code is not giving you any errors: 我敢打赌以下代码不会给您任何错误:

function parameterMap(data: any, operation: string) {
    if (operation === "create") {
        data.TypeName = 'ActiviteitEntity';
    }

    if (operation === "update" || operation === 'destroy') {
        data.Xml = contractors.htmlEncode(data.Xml);
    }               
}

But you will get errors if you try to do (sample 'a'): 但是,如果尝试这样做(示例“ a”), 则会出错:

var data:DataSourceTransportParameterMapData; 
parameterMap(data,'create'); 
console.log(data.TypeName); // Error TypeName does not exist on DataSourceTransportParameterMapData

Just because you pass a variable into a function (that accepts any ) doesn't change the external type of a variable. 仅仅因为将变量传递给函数(接受any )不会改变变量的外部类型。

To tell typescript about your extensions to a type you can always do: 要告诉打字稿您对某个类型的扩展名,可以随时执行以下操作:

interface DataSourceTransportParameterMapData { 
    TypeName?: string;
}

And since interfaces are open ended (multiple definitions are merged), this would cause something like sample 'a' to compile as well. 而且由于接口是开放式的(合并了多个定义),所以这也会导致类似样本“ a”的编译。

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

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