本文へ移動

k8o

お問い合わせ

255文字以内で入力してください

GitHub Issueからのお問い合わせもお待ちしております。

Blog

公開: 2023年7月10日(月)更新: 2026年11月9日(月)約9分で読めます

TypeScriptのsatisfiesで型が絞り込まれる理由

TypeScriptのsatisfiesは式の型を変えずに型を検査する演算子ですが、制約にリテラル型のユニオンがあると、その部分がリテラル型に絞り込まれます。起きる例と理由、絞り込まれた型に別の値を代入したときのエラーを紹介します。

はじめに

satisfiesは、式の型が指定した型に合うかを検査する演算子です。式の型は変えずに残し、TypeScript 4.9で追加されました。

変数の型を宣言した型そのものにするときは、型注釈を使います。型を検査しながら、推論されたリテラル型を残したいときにsatisfiesを使います。ただし、制約にリテラル型のユニオンがあると、satisfiesを付けた式の型が付けないときより絞り込まれます。

次の3つのpaletteは、どれもblueをbleuと書き間違えています(TypeScript Playground)。

ts
type Colors = "red" | "green" | "blue";

const palette1 = {
  red: "#ff0000",
  green: "#00ff00",
  bleu: "#0000ff"
} as const;

const palette2: Record<Colors, string> = {
  red: "#ff0000",
  green: "#00ff00",
  bleu: "#0000ff"
};

const palette3 = {
  red: "#ff0000",
  green: "#00ff00",
  bleu: "#0000ff"
} as const satisfies Record<Colors, string>;

palette1には型の制約が無いので、この誤りに気づけません。palette2は型注釈で、palette3はsatisfiesで制約を付けているので、bleuの行がObject literal may only specify known properties, and 'bleu' does not exist in type 'Record<Colors, string>'.(TS2353)になります。

bleuをblueに直すと、それぞれの型は次のように推論されます。

ts
// palette1
type Palette1 = {
  readonly red: "#ff0000";
  readonly green: "#00ff00";
  readonly blue: "#0000ff";
};

// palette2
type Palette2 = Record<Colors, string>;

// palette3
type Palette3 = {
  readonly red: "#ff0000";
  readonly green: "#00ff00";
  readonly blue: "#0000ff";
};

palette1とpalette3はリテラル型が残り、palette2は型注釈どおりのRecord<Colors, string>になります。satisfiesは、式の型を変えずに制約だけを付けます。

式の型が絞り込まれる例

次のuser1とuser2を比べます(TypeScript Playground)。

ts
type User = {
  id: string;
  type: 'teacher' | 'pupil',
  name: string;
  is_verified: boolean;
};

const user1 = {
  id: 'a',
  type: 'teacher',
  name: 'a a',
  is_verified: true,
};

const user2 = {
  id: 'a',
  type: 'teacher',
  name: 'a a',
  is_verified: true,
} satisfies User;

satisfiesは式の型を変えないはずなので、user2はuser1と同じ型になりそうです。ところが、推論される型は違います。

ts
// user1
type User1 = {
  id: string;
  type: string;
  name: string;
  is_verified: boolean;
};

// user2
type User2 = {
  id: string;
  type: "teacher";
  name: string;
  is_verified: true;
};

idとnameは同じですが、user2のtypeとis_verifiedは、'teacher'とtrueのリテラル型になっています。

配列でも、同じように絞り込まれます(TypeScript Playground)。

ts
type Numbers = (1 | 2 | 3 | 4)[];

const numbers1 = [1, 2, 3];

const numbers2 = [1, 2, 3] satisfies Numbers;

numbers1はnumber[]になりますが、numbers2は制約にリテラル型のユニオンがあるので、(1 | 2 | 3)[]まで絞り込まれます。

ts
// numbers1
type Numbers1 = number[];

// numbers2
type Numbers2 = (1 | 2 | 3)[];

as constを付けたときのような、タプル型にはなりません。

絞り込まれる理由

satisfiesは、右に書いた型を、左の式の文脈上の型(contextual type)として渡します。文脈上の型にリテラル型のユニオンがあると、TypeScriptは'teacher'のようなリテラルをstringに広げず、リテラル型のまま残します。TypeScriptのIssueでも、意図どおりの挙動だと説明されています(TypeScript#55189)。

user1のように制約が無ければ、'teacher'はstringに広げて推論されます。もしsatisfiesの検査でも広げると、'guardian'も'teacher'もstringになります。検査はstring extends 'teacher' | 'pupil'となり、どちらも制約を満たさないと判断されます。リテラル型のまま残すので、'guardian' extends 'teacher' | 'pupil'は満たさず、'teacher' extends 'teacher' | 'pupil'は満たす、と見分けられます。

is_verifiedの制約はbooleanですが、trueはbooleanに広げられません。booleanはtrue | falseのユニオンで、リテラル型のユニオンとして扱われるためです。

絞り込まれた型への代入

絞り込まれた型は、あとで値を書き換えるときに問題になります。user2をletで宣言して書き換えると、ユニオンのほかの値を代入した行がエラーになります。

ts
let user2 = {
  id: 'a',
  type: 'teacher',
  name: 'a a',
  is_verified: true,
} satisfies User;

user2.type = 'pupil'; // Type '"pupil"' is not assignable to type '"teacher"'.
user2.is_verified = false; // Type 'false' is not assignable to type 'true'.

あとで書き換える値は、const user3: User = { ... }のように型注釈で宣言すれば、'pupil'もfalseも代入できます。

ジェネリクスの制約

ジェネリクスでも、型引数の制約にリテラル型のユニオンがあると、同じようにリテラル型が残ります(TypeScript Playground)。

ts
type PrimitiveType = {
  test: string
};
type LiteralType = {
  test: 'a' | 'b';
};

const result = {
  test: 'a'
};

declare const test1: <T extends PrimitiveType>(arg: T) => T;
declare const test2: <T extends LiteralType>(arg: T) => T;

const result1 = test1({ test: 'a' });
const result2 = test2({ test: 'a' });

resultとresult1とresult2の型は、次のように推論されます。

ts
// result
type Result = {
    test: string;
};

// result1
type Result1 = {
    test: string;
};

// result2
type Result2 = {
    test: "a";
};

result2だけが、stringではなく'a'になります。test2のT extends LiteralTypeで、'a' | 'b'の制約が付いているためです。

おわりに

satisfiesやジェネリクスの制約にリテラル型のユニオンがあると、式のリテラル型はそのまま残ります。絞り込まれた型にはユニオンのほかの値を代入できないので、あとで書き換える値は型注釈で宣言します。