typescript - 符号是什么意思| 在打字稿中?
问题描述
我是 TypeScript 的新手,遇到了符号 | 在使用 Typescript 解决 LeetCode 问题时。我猜它定义了默认变量类型。有人可以详细说明一下。
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
解决方案
来自https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html:
联合类型描述的值可以是多种类型之一。我们使用竖线 (
|
) 来分隔每种类型,number | string | boolean
值的类型也可以是 anumber
、 astring
或 aboolean
。
推荐阅读
- python - 您如何模拟假期前后具有可变季节性的零售销售?
- python - 如何通过在现有列上应用函数来将列添加到 pyspark DataFrame?
- pandas - 如何使用 scipy.integrate.trapz() 集成系列
- c++ - C ++如何将对象移动到nullptr
- javascript - Vue Router 默认子路由最初未加载
- android - 获取存储在 Genymotion 模拟器中的文件
- assembly - 我在将这个将字符数组反转为 MIPS 指令的 C++ 代码转换时遇到了很多麻烦
- java - 预接触 JVM 堆很慢
- python - 试图在数据集中找到最优价格点
- c# - Wpf.PropertyGrid ComboBox 来自自定义属性中的字符串数组