我如何检查元素是否是U的一个实例? [英] How can I check if element is an instanceof U?

查看:89
本文介绍了我如何检查元素是否是U的一个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将类型的任何元素添加到数组中,然后从该数组中获取数字中的元素:

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

list.forEach(e => {
//我想检查e是否为U
类型// if(typeof(e)=== typeof U))//错误:不起作用
result.push(< U>< any> e);
});

返回<任何[]>结果;
}


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

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

alert(result.toString());

但它不允许我根据泛型检查元素的类型。



有没有办法做到这一点? =https://stackoverflow.com/users/536/judah-himango> Judah 指出单独使用泛型不太可能。我找到了一个解决方法,我发送了一个更多的类型参数...

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

list.forEach(e => {
//提取类名
//用于匹配原始类型
var typeName = / function\\ \\ s *([^(] *)/ i.exec(arg +)[1] .toLocaleLowerCase();

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

//如果它不是原始类型或不匹配类型
//尝试检查它是否为
的实例if(!isOfType)
{
试试{
isOfType =(e instanceof arg)
}
catch(ex){}
}

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

return< any []>结果;
}

用法:

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

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

小冗余,如果有人知道删除冗余的方法,请留下评论或编辑此代码。

或者,对于原始类型只有我可以使用 filter 作为Judah提到的。


I want to add elements of type any to an array and later get the elements from this array that are numbers:

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());

But it doesn't allow me to check the type of the elements against a generic type.

Is there a way to accomplish this?

解决方案

As Judah pointed out it is unlikely to be possible using generic types alone. I found a workaround where I send in one more parameter with the type...

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;
}

Usage:

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

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

Little redundancy, if someone know a way to remove this redundancy please leave a comment or edit this code.

Or, for primitive types only I could use filter as Judah mentioned.

这篇关于我如何检查元素是否是U的一个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆