typescript - How to check if array of class?
问题描述
Let's say I have a class named Form.
export class Form {}
How can I check if a variable is an array of Form
?
const foo: Form = new Form()
if (foo instanceof Form) {}
const baz: Form[] = [new Form(), new Form()]
?
解决方案
You can use Array.prototype.every
to check if every single entry in your baz
array is indeed an instance of Form
:
const baz = [new Form(), new Form()];
console.log(baz.every(entry => entry instanceof Form)); // true
const baz2 = [new Form(), new Form(), ''];
console.log(baz2.every(entry => entry instanceof Form)); // false
推荐阅读
- jquery - AJAX partial loading page inside div problem
- typescript - 如何内联快速初始化打字稿数据结构
- html - 无法通过 VBA 在 Internet Explorer 中选择下拉列表
- node.js - 如何实现一个管理员审批系统,用于在 node.js 中发布博客并表达应用程序?
- c++ - 设置 viewController 共享公共对象
- nativescript - 如何使用 Appium 将 browserstack 与 Nativescript 集成?
- python - 使用 Python 实现最小堆
- flutter - 如何在 Flutter 中制作 Arc(Curve)轮播(ListVie,Row Widget)?
- airflow - 运行气流网络服务器时创建的新气流目录
- python - 系统找不到用dryscrape指定的文件