首页 > 解决方案 > 任何 [] 函数参数的打字稿类型保护

问题描述

想知道让这个类型保护实例对这样的函数进行检查是否有意义:

Foo(value: any[]) {
    if (value instanceof Array) {
        Console.log('having an 
        array')
    } 
}

由于参数已被声明为任何数组,这是否自动暗示运行时值应该始终是数组的实例?

这是涉及任何特定情况并且类型保护是必要的吗?

我的意思是:指定参数的类型是否确保运行时该参数在开发人员工具中显示为该类型?在这种情况下,该类型的实例表示无用的检查..

标签: angulartypescripttypeguards

解决方案


Your if condition inside Foo() is of no significance as while developing compiler with throw an error if an array is not passed as param in the method.

Typeguards are only significant at compile time, because your transpiled code in javascript is shipped to the browser.


推荐阅读