2013-06-30 54 views
3

我想any類型的元素添加到一個數組,後來得到這個數組是數字的元素:如何檢查元素是否爲U的一個實例?

function OfType<T, U>(list: T[]) : U[] 
{ 
    var result: U[] = []; 

    list.forEach(e => { 
     // I want to check if e is of type U 
     //if (typeof(e) === typeof(U)) // ERROR: doesn't work 
      result.push(<U><any>e); 
    }); 

    return <any[]>result; 
} 


var list: any[] = []; 
list.push("A"); 
list.push(2); 

var result = OfType<any, number>(list); 

alert(result.toString()); 

不過,這並不讓我檢查元素的類型與泛型類型。

有沒有辦法做到這一點?

回答

1

由於Judah指出,單獨使用泛型不太可能。我發現了一個解決辦法,我在一個參數發送與該類型...

function OfType<T, U>(list: T[], arg: Function) : U[] 
{ 
    var result: U[] = []; 

    list.forEach(e => { 
     // extract the name of the class 
     // used to match primitive types 
     var typeName = /function\s*([^(]*)/i.exec(arg+"")[1].toLocaleLowerCase(); 

     var isOfType = typeof(e) === typeName; 

     // if it is not primitive or didn't match the type 
     // try to check if it is an instanceof 
     if (!isOfType) 
     { 
      try { 
       isOfType = (e instanceof arg) 
      } 
      catch (ex) { } 
     } 

     if (isOfType) 
      result.push(<U><any>e); 
    }); 

    return <any[]>result; 
} 

用法:

var numbers = OfType<any, number>(list, Number); 
var foos = OfType<any, Foo>(list, Foo); 

alert("Numbers: " + numbers); 
alert("Foos: " + foos); 

小冗餘,如果有人知道的方法來消除這種冗餘,請發表評論或編輯此代碼。

或者,對於原始類型,我只能用猶太人提到的filter

1

Javascript typeof適用於對象實例,而不是類型本身。 (畢竟,TypeScript泛型在編譯後的JavaScript中消失了。)

您需要獲取U的一個實例,然後調用typeof,並將其與typeof(e)進行比較。

請記住,JavaScript中的類型信息並不像.NET框架中那樣豐富。即使對象是Foo類的一個實例,typeof(myCustomObject)也會返回'object'。

在你的情況,你試圖建立一個.OfType方法,將數組過濾到一個數字。你可以這樣寫:

var list: any[] = []; 
list.push("A"); 
list.push(2); 
var numbers = list.filter(e => typeof(e) === "number"); 
相關問題