以下是情景:
type Admin = {
name : string,
privileges : string[]
}
type Employee ={
name : string,
startDate : Date
}
type ElevatedEmployee = Admin & Employee;
const el : ElevatedEmployee ={ // all the properties must be implemented here else compiler will complain
name : 'Max',
privileges :['create server'],
startDate : new Date()
}
我可以使用接口来实现相同的功能:
interface Admin {
name : string,
privileges : string[]
}
interface Employee {
name : string,
startDate : Date
}
interface Combined extends Admin, Employee {
//this doesn't throw me a compilation error, even though i dont implement those properties here
}
什么是交叉类型和接口?何时使用哪个?