Asta Nguyen • December 21, 2024
TypeScript provides two main ways to define the shape of an object: type and interface. While both can be used interchangeably in many cases, there are specific scenarios where type offers more flexibility and power. This blog will explore these scenarios and explain why you might prefer type over interface.
Key Differences:
type can define union types, which interface cannot.
type Result = Success | Failure;
type can define intersection types.
type Combined = TypeA & TypeB;
type can alias primitive types.
type StringAlias = string;
type can define tuples
type Tuple = [number, string];
type can create mapped types
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
type can define more complex type constructs like conditional types.
type Conditional<T> = T extends string ? string : number;
Let's consider a React component where we define the props using type instead of interface.
// Case type
type ButtonProps = {
label: string;
onClick: () => void;
variant: 'primary' | 'secondary';
};
const Button: React.FC<ButtonProps> = ({ label, onClick, variant }) => {
return (
<button className={`btn-${variant}`} onClick={onClick}>
{label}
</button>
);
};
// Case interface
// This is not possible with interface
interface ButtonProps {
label: string;
onClick: () => void;
variant: 'primary' | 'secondary'; // Error: String literal types are not allowed in interfaces
}
When you need to combine multiple types, type is more flexible.
type BaseProps = {
id: string;
};
type AdvancedProps = {
isActive: boolean;
};
type CombinedProps = BaseProps & AdvancedProps;
const Component: React.FC<CombinedProps> = ({ id, isActive }) => {
return (
<div id={id} className={isActive ? 'active' : ''}>
Content
</div>
);
}
Using interface (More Verbose)
interface BaseProps {
id: string
}
interface AdvancedProps {
isActive: boolean
}
interface CombinedProps extends BaseProps, AdvancedProps {}
const Component: React.FC<CombinedProps> = ({ id, isActive }) => {
return (
<div id={id} className={isActive ? 'active' : ''}>
Content
</div>
);
};
When you need to define a tuple type, type is the only option.
type TupleProps = {
coordinates: [number, number];
};
const CoordinateDisplay: React.FC<TupleProps> = ({ coordinates }) => {
return (
<div>
X: {coordinates[0]}, Y: {coordinates[1]}
</div>
);
};
Using interface (Not Possible)
When you need to create a mapped type, type is more suitable.
type ReadonlyProps<T> = {
readonly [P in keyof T]: T[P];
};
type UserProps = {
name: string;
age: number;
};
type ReadonlyUserProps = ReadonlyProps<UserProps>;
const UserComponent: React.FC<ReadonlyUserProps> = ({ name, age }) => {
return (
<div>
Name: {name}, Age: {age}
</div>
);
};
Using interface (Not Possible)
// This is not possible with interface
interface ReadonlyProps<T> {
readonly [P in keyof T]: T[P]; // Error: Mapped types are not allowed in interfaces
}
While interface is useful for defining object shapes that are intended to be extended or implemented by classes, type offers more versatility and power for defining complex types. By understanding the strengths of type, you can make more informed decisions in your TypeScript projects.
In summary, use type when you need:
This flexibility makes type a better choice in many scenarios.