types
are indexed by the default whereas interfaces
are not. It makes interfaces
more safe.
Consider this example:
type Admin = {
name: string,
privileges: string[]
}
type Employee = {
name: string,
startDate: Date
}
type CombinedType = Admin & Employee;
interface CombinedInterface extends Admin, Employee { }
const interfaceData: CombinedInterface = { name: '', privileges: [], startDate: new Date() }
const typeData: CombinedType = { name: '', privileges: [], startDate: new Date() }
const handleType = (obj: Record<string, unknown>) => {
obj['name'] = 42 // unsafe
}
handleType(interfaceData) // error
handleType(typeData) // ok
Source: https://stackoverflow.com/questions/72598769/intersection-types-vs-interfaces-in-typescript