如何在Typescript中检查数组的类型? [英] How can I check the type of an Array in Typescript?

查看:97
本文介绍了如何在Typescript中检查数组的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下签名的功能

I have a function with the following signature

public async sequenceAnimations(animations: AnimationPlayer[] | AnimationTracker[]): Promise<any>

在函数本身中,我想基于它是AnimationPlayer数组还是AnimationTracker数组进行分支,所以我尝试了以下方法:

In the function itself I want to branch based on if it is an AnimationPlayer array or an AnimationTracker array so I tried this:

let mappedAnimations = animations;
if (animations instanceof Array<AnimationTracker>) {
    mappedAnimations = animations.map(anim => anim.animationPlayer)
}

如您所见,我正在尝试允许调用者传递具有AnimationPlayer实例的AnimationPlayer数组或AnimationTracker数组.但是在检查类型为Array的instanceof时出现错误

As you can see I am trying to allow the caller to pass either an AnimationPlayer array or an AnimationTracker array which has an instance of animationPlayer. But I get an error when checking instanceof the Array with a type

"instanceof"表达式的右侧必须为"any"类型或可分配给"Function"接口类型的类型.

The right-hand side of an 'instanceof' expression must be of type 'any' or of a type assignable to the 'Function' interface type.

此外,自动完成功能也不会在if块中注册数组的类型,所以我认为我无法像这样检查数组类型.

Also the autocomplete isn't registering the type of the array in the if-block so I assume I can't check the array type like this.

确定要传递的数组类型是什么的正确方法是什么?

What is the proper way to determine what the type of array being passed is?

推荐答案

您不能对具有类型参数的泛型类型使用 instanceof .编译后,所有泛型都将删除,因此 Array的动画实例 会变成 Array的动画实例,这将无法实现您的期望.

You can't use instanceof with a generic type with type parameters. After compilation all generics are erased so animations instanceof Array<AnimationTracker> would turn into animations instanceof Array which would not do what you expect it to do.

由于没有键入Javscript数组,因此没有内置的方法可以区分 AnimationPlayer [] AnimationTracker [] ,如果数组为空,则在运行时他们真的很难区分.但是,您可以创建一个自定义类型防护,该防护使用数组中的第一个非空项目来确定类型.对于空数组,它将始终返回 false ,但是在大多数情况下,这可能是一个好的解决方案:

Since in Javscript arrays are not typed there is no build-in way of differentiate between AnimationPlayer[] and AnimationTracker[] and if the arrays are empty, at runtime they are really indistinguishable. You can however create a custom type guard that uses the first non-null item in the array to determine the type. For empty arrays this would always return false but it might be an ok solutions in most cases:

function isArrayOf<T>(array:any[], cls: new (...args: any[]) => T) : array is T[] {
    for(let item of array) {
        if(item != null) return  item instanceof cls;
    }
    return  false;
}
async function sequenceAnimations(animations: AnimationPlayer[] | AnimationTracker[]): Promise<any> {
    let mappedAnimations = animations;
    if (isArrayOf(animations, AnimationTracker)) {
        // animations is AnimationTracker[]
        mappedAnimations = animations.map(anim => anim.animationPlayer);
    }
}

这篇关于如何在Typescript中检查数组的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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