【问题标题】:why typescript string & number show never?为什么打字稿字符串和数字从不显示?
【发布时间】:2021-12-21 17:10:43
【问题描述】:

我正在学习 Typescript,但我有些困惑。

type t = number & string // never
type t1 = number & boolean // never
type t2 = number & string[] // number & string[]

为什么看起来不一样?

【问题讨论】:

标签: typescript


【解决方案1】:

在 Typescipt 中,两个基本类型的交集 & 永远不会,因为变量不能既是字符串又是数字,但是两个数组/对象的交集被称为 branded object 一个带有原始类型的数组/对象是有效类型,称为branded primitve,那么我们在哪里使用品牌原始类型? , 参考下面的例子

type SomeUrl = string & {'this is a url': {}};

let x = <SomeUrl>'';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 2021-12-08
    • 1970-01-01
    相关资源
    最近更新 更多