angular - 任何 [] 函数参数的打字稿类型保护
问题描述
想知道让这个类型保护实例对这样的函数进行检查是否有意义:
Foo(value: any[]) {
if (value instanceof Array) {
Console.log('having an
array')
}
}
由于参数已被声明为任何数组,这是否自动暗示运行时值应该始终是数组的实例?
这是涉及任何特定情况并且类型保护是必要的吗?
我的意思是:指定参数的类型是否确保运行时该参数在开发人员工具中显示为该类型?在这种情况下,该类型的实例表示无用的检查..
解决方案
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.
推荐阅读
- excel - 添加 PowerQuery 时如何使用字符串作为源
- android - AndroidRuntime:致命异常:AsyncTask #1 进程
- sql - 为“aa”多次指定了“PATIENT_ID”列
- kotlin-multiplatform - kotlin-multiplatform 时间戳以毫秒为单位(unix 时间)
- c# - 我的 .exe 在其他计算机上打开和关闭
- c# - Parse 方法抛出异常
- python - pandas 从累积的多类别列中计算每日总数
- postgresql - PostgreSql 查询大表时的问题
- filter - 如何在logstash中使用聚合函数来计算“经过”时间字段之间的差异?
- vue.js - VueX:如何在一个循环中提交?