as-const-satisfieslisted
Install: claude install-skill ncaq/konoka
# `as const satisfies` パターンの使用
## 基本原則
定数定義では`as const`を積極的に使用します。
`as const`によりリテラル型が保持され、
より厳密な型推論が可能になります。
名前のついている特定の型で型チェックも出来る場合は`as const satisfies Type`を使用します。
これで「`as const`を使いたいが、型のチェックもしたい」という要求を満たせます。
## 使い分け
### `as const`を使う場合
定数定義では基本的に`as const`を使います。
リテラル型の保持と型レベルでの`readonly`(ただし実行時にはオブジェクトは凍結されません)が得られます。
```typescript
const status = {
pending: "pending",
approved: "approved",
rejected: "rejected",
} as const;
// `status.pending`の型: "pending"
```
### `as const satisfies Type`を使う場合
`as const`の利点を保ちつつ型チェックも行いたい場合に使用します。
```typescript
const status = {
pending: "pending",
approved: "approved",
rejected: "rejected",
} as const satisfies Record<string, string>;
// `status.pending`の型: "pending"(リテラル型保持)
// かつ`Record<string, string>`を満たすことが保証される
```
### `: Type` や `satisfies Type` を使う場合
可変性が必要な場合や、
リテラル型の保持が問題になる場合に限り使用します。
```typescript
// 可変な配列が必要な場合
const items: string[] = [];
items.push("a"); // OK
// `as const`だと`readonly`になり`push`できない
```
## `as const satisfies`がそのまま使えないパターン
`satisfies`で指定する型が可変な型の場合、
`as const`の`readonly`性と競合してエラーになることがあります。
### 問題のあるパターン
```typescript
type Config = {
values: string[];
};
// エラー: `readonly string[]`は`string[]`に割り当てられない
const config = {
values: ["a", "b", "c"],
} as const satisfies Config;
```
### 解決方法: `readonly`な型を定義する
`satisfies`で使用する型自体を`readonly`にすることで解決できます。
```typescript
type Config = {
readonly values: readonly string[];
};
const config = {
values: ["a", "b", "c"],
} as cons