Item 32: Avoid Including null or undefined in Type Aliases
May 10, 2024 ยท View on GitHub
Things to Remember
- Avoid defining type aliases that include
nullorundefined.
Code Samples
function getCommentsForUser(comments: readonly Comment[], user: User) {
return comments.filter(comment => comment.userId === user?.id);
}
type User = { id: string; name: string; } | null;
interface User {
id: string;
name: string;
}
type NullableUser = { id: string; name: string; } | null;
function getCommentsForUser(comments: readonly Comment[], user: User | null) {
return comments.filter(comment => comment.userId === user?.id);
}
type BirthdayMap = {
[name: string]: Date | undefined;
};
type BirthdayMap = {
[name: string]: Date | undefined;
} | null;