Quantcast
Channel: 黑暗執行緒
Viewing all articles
Browse latest Browse all 2421

【茶包射手日記】TypeScript錯誤: missing property 'concat'

$
0
0

一段運作正常的Kendo UI JavaScript程式,搬到TypeScript後發生編譯錯誤,起火點在DataSourceOptions物件的group屬性:

var x = new kendo.data.DataSource({
                data: [],
                group: {
                    field: "Boo", aggregates: [
                        { field: "Foo", aggregate: "sum" }
                    ]
                }
            });

錯誤訊息如下

Supplied parameters do not match any signature of call target:
    Types of property 'group' of types '{ data: any[]; group: { field: string; aggregates: { field: string; aggregate: string; }[]; [n: number]: kendo.data.DataSourceGroupItem; }; }' and 'kendo.data.DataSourceOptions' are incompatible:
        Type '{ field: string; aggregates: { field: string; aggregate: string; }[]; [n: number]: kendo.data.DataSourceGroupItem; }' is missing property 'concat' from type 'kendo.data.DataSourceGroupItem[]'.   

江湖經驗不足,沒能一眼看出錯誤原因,到官方論壇提問後才自己找出問題來源。is missing property 'concat'是關健字,通常發生於該傳陣列的地方卻給了一般物件。

查看kendo.all.d.ts(官方論壇可下載最新版),group的定義為DataSourceGroupItem[];

interface DataSourceOptions<T extends kendo.data.Model> {
        aggregate?: DataSourceAggregateItem[];
        autoSync?: bool;
        batch?: bool;
        data?: any;
        filter?: any;
        group?: DataSourceGroupItem[];
        page?: number;
        pageSize?: number;

但依據API文件,group可為陣列或物件(如果只有一筆,可直接給設定物件),與TypeScript指定必須為DataSourceGroupItem[]有出入,如此可解釋為何JavaScript程式能跑,搬到TypeScript卻無法編譯。依我的理解,屬性型別不像函式可以多載,設定any的話就無法規範DataSourceGroupItem的設定屬性,或許這是Kendo UI RD的取捨吧!

既知原因,解決就是小事一椿,將物件改寫成單一元素陣列 group: [ { field: "boo", aggregates: [ { field: "foo", aggregate: "sum" } ] } ] 便一切OK囉~


Viewing all articles
Browse latest Browse all 2421

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>