Code ReviewIntermediate~15 min

Review: null/undefined Handling

typescriptnull-safetyerror-handling

Code Review Task

Please review the following TypeScript code.

Background

This is a user profile display feature implementation. The component displays data fetched from an API, but null/undefined handling is insufficient.

Review Criteria

  • null/undefined handling
  • Runtime error risks
  • Optional Chaining utilization
  • Nullish Coalescing utilization

Your Answer

userProfile.tstypescript
interface User {
id: number;
name: string;
profile: {
avatar: string;
bio: string;
} | null;
settings: {
notifications: boolean;
theme: string;
};
}
function displayUserInfo(user: User): string {
// Display user information
const avatar = user.profile.avatar;
const bio = user.profile.bio || 'No bio';
const notifications = user.settings.notifications;
return `
Name: ${user.name}
Avatar: ${avatar}
Bio: ${bio}
Notifications: ${notifications}
`;
}
export { displayUserInfo };
行番号をクリックしてコメントを追加(Shift+クリックで範囲選択)

Minimum 100 characters required

0

Add comments and enter at least 100 characters in the summary

Model Answer

Submit your answer or click "Show" to view the model answer.