📝 ぴえろぐ
💻 Tech

TypeScript開発で役立つTips

TypeScriptの開発で役立つTipsをまとめました


TypeScript開発で役立つTips

TypeScriptを使った開発で便利なテクニックを紹介します。

1. Utility Types の活用

TypeScriptには便利なユーティリティ型が用意されています。

```typescript interface User { id: number; name: string; email: string; age: number; }

// 一部のプロパティのみ必須にする type PartialUser = Partial;

// 一部のプロパティのみ選択する type UserPreview = Pick<User, ‘id’ | ‘name’>;

// 一部のプロパティを除外する type UserWithoutEmail = Omit<User, ‘email’>; ```

2. 型ガードの使用

```typescript function isString(value: unknown): value is string { return typeof value === ‘string’; }

const processValue = (value: unknown) => { if (isString(value)) { // ここではvalueはstring型として扱える console.log(value.toUpperCase()); } }; ```

3. const assertion

```typescript // 通常の配列(型は string[]) const colors1 = [‘red’, ‘blue’, ‘green’];

// const assertion(型は readonly [‘red’, ‘blue’, ‘green’]) const colors2 = [‘red’, ‘blue’, ‘green’] as const; ```

これらのTipsを活用して、より型安全なコードを書きましょう!

🔗関連記事