剑道,TypeScript定义错误:

Kendo, TypeScript definition bug:

本文关键字:错误 定义 TypeScript 剑道      更新时间:2023-09-26

我正在TypeScript文件中使用以下语法创建剑道图(取自http://demos.telerik.com/kendo-ui/bar-charts/index):

function createChart() {
            $("#chart").kendoChart({
                title: {
                    text: "Site Visitors Stats 'n /thousands/"
                },
                legend: {
                    visible: false
                },
                seriesDefaults: {
                    type: "bar"
                },
                series: [{
                    name: "Total Visits",
                    data: [56000, 63000, 74000, 91000, 117000, 138000]
                }, {
                    name: "Unique visitors",
                    data: [52000, 34000, 23000, 48000, 67000, 83000]
                }],
                valueAxis: {
                    max: 140000,
                    line: {
                        visible: false
                    },
                    minorGridLines: {
                        visible: true
                    },
                    labels: {
                        rotation: "auto"
                    }
                },
                categoryAxis: {
                    categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
                    majorGridLines: {
                        visible: false
                    }
                },
                tooltip: {
                    visible: true,
                    template: "#= series.name #: #= value #"
                }
            });
        }

当我这样做时,我得到了以下错误,这似乎表明ChartOptions应该是一个数组:

"错误70类型为‘{title:{text:string;};图例:{可见:布尔值;};seriesDefault:{type:string;};ser…'不是可分配给"ChartOptions"类型的参数。财产类型"categoryAxis"不兼容。类型"{[x:number]:未定义;类别:string[];majorGridLines:{visible:boolean;};}"不可分配给类型"ChartCategoryAxisItem[]"。类型"{[x:number]:undefined;categories:string[];majorGridLines:{visible:boolean;};}"中缺少属性"length"。"

如果我修改createChart函数,如下所示,错误会消失,但图表不会呈现:

function createChart() {
            $("#chart").kendoChart([{ //Notice opening array bracket
                title: {
                    text: "Site Visitors Stats 'n /thousands/"
                },
                legend: {
                    visible: false
                },
                seriesDefaults: {
                    type: "bar"
                },
                series: [{
                    name: "Total Visits",
                    data: [56000, 63000, 74000, 91000, 117000, 138000]
                }, {
                    name: "Unique visitors",
                    data: [52000, 34000, 23000, 48000, 67000, 83000]
                }],
                valueAxis: {
                    max: 140000,
                    line: {
                        visible: false
                    },
                    minorGridLines: {
                        visible: true
                    },
                    labels: {
                        rotation: "auto"
                    }
                },
                categoryAxis: {
                    categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
                    majorGridLines: {
                        visible: false
                    }
                },
                tooltip: {
                    visible: true,
                    template: "#= series.name #: #= value #"
                }
            }]); // And closing array
        }

其他人遇到过这个问题吗?在我看来是定义文件中的一个bug?

我能够通过将valueAxis和categoryAxis值定义为数组来解决问题,如下所示:

function createChart() {
            $("#chart").kendoChart({
                title: {
                    text: "Site Visitors Stats 'n /thousands/"
                },
                legend: {
                    visible: false
                },
                seriesDefaults: {
                    type: "bar"
                },
                series: [{
                    name: "Total Visits",
                    data: [56000, 63000, 74000, 91000, 117000, 138000]
                }, {
                    name: "Unique visitors",
                    data: [52000, 34000, 23000, 48000, 67000, 83000]
                }],
                valueAxis: [{
                    max: 140000,
                    line: {
                        visible: false
                    },
                    minorGridLines: {
                        visible: true
                    },
                    labels: {
                        rotation: "auto"
                    }
                }],
                categoryAxis: [{
                    categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
                    majorGridLines: {
                        visible: false
                    }
                }],
                tooltip: {
                    visible: true,
                    template: "#= series.name #: #= value #"
                }
            });
        }

我为这个类型定义创建了一个补丁,它允许您使用确切的代码。

它太大了,无法放置在Stack Overflow上,所以补丁在这里:https://github.com/Steve-Fenton/DefinitelyTyped/blob/patch-1/kendo-ui/kendo-ui.d.ts

如果你的代码与这个定义一起工作,我可以向绝对类型的项目发送一个pull请求,让它更新(所以它将作为NuGet包等提供)。