我一直在读一本书的有效Typescript ,有一段代码我不清楚:
1.
interface Person {
name: string;
}
interface Lifespan {
birth: Date;
death?: Date;
}
type PersonSpan = Person & Lifespan; // why not never type ?
&
运算符计算两种类型的交集.什么样的价值观属于
PersonSpan类型的?
乍一看,Person和LifeSpan界面没有 属性,因此您可能会认为它是空集(即,Never 类型).But type operations apply to the sets of values (the domain of the type), not to the properties in the interface.,并记住具有附加属性的值 仍然属于一种类型.因此,一个既具有人的属性又具有生命周期 的值 将属于交叉点类型:
const ps: PersonSpan = {
name: 'Alan Turing',
birth: new Date('1912/06/23'),
death: new Date('1954/06/07'),
}; // OK
-
如果宣布
输入newPersonSpan=Person|LifeSpan;//我们现在可以分配哪些值?
有人能用通俗一点的语言解释一下这件事吗