2

我一直致力于为 flot 创建环境定义文件作为学习一些 TypeScript 的练习,但我在 flot 的文档中多次遇到这个问题(在轴选项中):

ticks: null or number or ticks array or (fn: axis -> ticks array) 

所以我可以在我的axisOptions界面中做到这一点:

interface axisOptions {
    ticks?: any;
}

这涵盖了所有可能的选项,但有没有更好的方法将其限制为数字、数组(数字)或函数,而不是其他任何东西?

4

2 回答 2

3

您目前不能指定多种类型 - 实际上这是动态any类型的完美使用,因为虽然它不是“任何东西”,但它肯定是动态的。

要在动态类型上强制执行类型,您必须检查它 - 就像在这个例子中一样:

function example (input?: any) {
    alert(typeof input);
    if (typeof input !== 'undefined' && typeof input !== 'string' && typeof input !== 'number') {
        alert('no');
        return;
    }

    alert('yes');
}

example(true);
example('Okay');
example();
于 2012-10-12T14:10:34.080 回答
0

您可以通过拥有多个方法定义来完成您所追求的一些事情

interface lodash extends lodashProto, lodashStatic {
  (value: Array): lodashCurried;
  (value: Object): lodashCurried;
  (value: string): lodashCurried;
  VERISON: string;
}
于 2012-10-15T13:01:15.137 回答