Skip to content

Latest commit

 

History

History
72 lines (49 loc) · 2.27 KB

null-in-type.md

File metadata and controls

72 lines (49 loc) · 2.27 KB

Item 32: Avoid Including null or undefined in Type Aliases

Things to Remember

  • Avoid defining type aliases that include null or undefined.

Code Samples

function getCommentsForUser(comments: readonly Comment[], user: User) {
  return comments.filter(comment => comment.userId === user?.id);
}

💻 playground


type User = { id: string; name: string; } | null;

💻 playground


interface User {
  id: string;
  name: string;
}

💻 playground


type NullableUser = { id: string; name: string; } | null;

💻 playground


function getCommentsForUser(comments: readonly Comment[], user: User | null) {
  return comments.filter(comment => comment.userId === user?.id);
}

💻 playground


type BirthdayMap = {
  [name: string]: Date | undefined;
};

💻 playground


type BirthdayMap = {
  [name: string]: Date | undefined;
} | null;

💻 playground