【问题标题】:What is the meaning of the symbol | in typescript?符号是什么意思|在打字稿中?
【发布时间】:2021-03-27 17:13:57
【问题描述】:

我是 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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-11-15
      • 2021-04-24
      • 2018-09-12
      • 2021-04-21
      • 2021-01-02
      • 1970-01-01
      • 2019-12-19
      • 2016-12-02
      相关资源
      最近更新 更多