提问人:Hyeonjae 提问时间:11/14/2023 更新时间:11/14/2023 访问量:36
如何在 typscript 中使用联合类型?[复制]
How to use union type in typscript? [duplicate]
问:
interface A {
name : string;
age : number;
}
interface B {
name: string;
address: string;
}
public samplefunction(data : A | B) : A | B{
switch(data.name)
case("John"):
console.log(data.age), // ✖ Property 'age' does not exist on type...
case("Kim"):
console.log(data.address), //✖ Property 'address' does not exist on type
....
}
为什么console.log错误时会出现错误?
在此示例代码中,我想知道如何使用用户联合类型。
这种联合类型方法是否正确?
了解联合类型
答: 暂无答案
评论
extends
A
B
data
age
A | B
A
age
"John"
address
"Kim"