I'm struggling to find the definition of the &
operator in TypeScript. I have recently come across the following code:
type IRecord<T> = T & TypedMap<T>;
What does that operator do, and how is it different from the union type |
?
I'm struggling to find the definition of the &
operator in TypeScript. I have recently come across the following code:
type IRecord<T> = T & TypedMap<T>;
What does that operator do, and how is it different from the union type |
?
Worth noting that if you'd prefer to use interfaces over types (although they're largely similar) that you can typically use interface extension instead of type intersection like this:
// base type
interface Shape {
color: string;
}
// extension
interface Square extends Shape {
sideLength: number;
}
// intersection
type Square = Shape & {
sideLength: number;
}
See Also: Difference between extending and intersecting interfaces in TypeScript?