定义文件:属性的多种可能类型

use*_*702 8 typescript

我正在为现有的JS库(CKEditor)编写一些定义.是否有可能更具体toolbar: any

文档:

toolbar:数组/字符串

工具箱(别名工具栏)定义.它是一个工具栏名称或工具栏(条带)数组,每个工具栏也是一个数组,包含一个UI项列表.

图书馆代码:

var toolbar = editor.config.toolbar;

// If it is a string, return the relative "toolbar_name" config.
if ( typeof toolbar == 'string' )
    toolbar = editor.config[ 'toolbar_' + toolbar ];

return ( editor.toolbar = toolbar ? populateToolbarConfig( toolbar ) : buildToolbarConfig() );
Run Code Online (Sandbox Code Playgroud)

Sim*_*ver 16

Typescript 1.4现在支持联合类型

当然,您仍然需要检查函数内部的值并做出相应的反应,但现在您可以在不必将类型更改为的情况下进行编译时检查any.

function f(x: number | number[]) {
  if (typeof x === "number") {
    return x + 10;
  }
  else {
    // return sum of numbers
  }
}
Run Code Online (Sandbox Code Playgroud)

http://blogs.msdn.com/b/typescript/archive/2015/01/16/announcing-typescript-1-4.aspx