首页 > 解决方案 > 符号是什么意思| 在打字稿中?

问题描述

我是 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)
 *     }
 * }
 */

标签: typescript

解决方案


来自https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html

联合类型描述的值可以是多种类型之一。我们使用竖线 ( |) 来分隔每种类型,number | string | boolean值的类型也可以是 a number、 astring或 a boolean


推荐阅读