コードレビュー中級15

レビュー: null/undefinedの処理

typescriptnull-safetyerror-handling

コードレビュー課題

以下のTypeScriptコードをレビューしてください。

背景

ユーザープロフィール表示機能の実装です。APIから取得したデータを表示するコンポーネントですが、null/undefinedの処理が不十分です。

レビュー観点

  • null/undefinedのハンドリング
  • 実行時エラーのリスク
  • Optional Chainingの活用
  • Nullish Coalescingの活用

あなたの回答

userProfile.tstypescript
interface User {
id: number;
name: string;
profile: {
avatar: string;
bio: string;
} | null;
settings: {
notifications: boolean;
theme: string;
};
}
function displayUserInfo(user: User): string {
// ユーザー情報を表示
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+クリックで範囲選択)

最低100文字必要です

0

コメントを追加し、サマリーを100文字以上入力してください

模範解答

回答を送信するか、「表示する」をクリックすると模範解答が表示されます。