In listing the differences between type
and interface
within Typescript, it only mentions the different syntax.
Now up till now, I've been using type
notation as my default, however I've had some situation where I want to define more specific types based on a "Base" type, which I'm not able to achieve using type
, but apparently am able to achieve using interface
.
Given the following code, would there be a way with using type
to maintain type-safety as appears to happen with interface
or is this an actual difference between type
and interface
that I can't find in the documentation?
type Vehicle = {
wheelCount: number;
size: 'small' | 'medium' | 'large'
}
type ExtendUsingType = Vehicle & {
// This can be any value, doesn't even have to be of type string
size: 'no-type-safety';
}
interface ExtendUsingInterface extends Vehicle {
// it has to be either 'small', 'medium' or 'large'
size: 'this-will-error';
}