Code Fix

上級

型注釈(: Type)はプロパティを消さずに「見えなく」する。satisfiesとの違い

変数に: Typeという型注釈を付けると、その型に無いプロパティへのアクセスができなくなります。実行時にはプロパティ自体は残っていますが、型の上では「そんなものは無い」ものとして扱われます。satisfiesは元の値の型をそのまま保つため、この制限を受けません。

エラーメッセージの読み方

main.ts(4,20): error TS2339: Property 'z' does not exist on type 'Point'.
main.ts
ファイル名
4
行番号
20
列番号 — この位置でtscが検査に失敗しました
TS2339
エラーコード — 検索するとTypeScriptの解説が見つかります
Property 'z' does not exist on type 'Point'.
内容 — 期待していたもの、または受け付けられなかったもの

このエラーが出る典型パターン

パターン1

 1  type Point = { x: number; y: number };
 2  const raw = { x: 0, y: 0, z: 5 };
 3  const origin: Point = raw;         
                     ^
 4  console.log(origin.z);
main.ts(4,20): error TS2339: Property 'z' does not exist on type 'Point'.

: Pointという型注釈を付けると、origin自体の型がPointに絞られ、実際には残っているzへアクセスできなくなります。satisfiesならrawの元の型がそのまま保たれ、zにもアクセスできます。

直し方: const origin: Point = raw; を const origin = raw satisfies Point; にします。

この問題を解いてみる →

広告
広告スロット(未設定)

パターン2

 1  type Item = { name: string };
 2  const raw = { name: "Pen", price: 100 };
 3  const item: Item = raw;         
                    ^
 4  console.log(item.price);
main.ts(4,18): error TS2339: Property 'price' does not exist on type 'Item'.

: Itemという型注釈を付けるとitem自体の型がItemに絞られ、実際には残っているpriceへアクセスできなくなります。satisfiesならrawの元の型が保たれます。

直し方: const item: Item = raw; を const item = raw satisfies Item; にします。

この問題を解いてみる →

パターン3

 1  type Config = { mode: string };
 2  const raw = { mode: "dark", debug: true };
 3  const cfg: Config = raw;         
                    ^
 4  console.log(cfg.debug);
main.ts(4,17): error TS2339: Property 'debug' does not exist on type 'Config'.

: Configという型注釈はcfgの型をConfigに絞り込みます。実行時にはdebugが残っていても、型の上ではアクセスできません。satisfiesなら元の型のままdebugにアクセスできます。

直し方: const cfg: Config = raw; を const cfg = raw satisfies Config; にします。

この問題を解いてみる →

パターン4

 1  type Shape2 = { kind: string };
 2  const raw = { kind: "circle", radius: 5 };
 3  const shape: Shape2 = raw;         
                     ^
 4  console.log(shape.radius);
main.ts(4,19): error TS2339: Property 'radius' does not exist on type 'Shape2'.

: Shape2という型注釈を付けるとshape自体の型がShape2に絞られ、実際には残っているradiusへアクセスできなくなります。

直し方: const shape: Shape2 = raw; を const shape = raw satisfies Shape2; にします。

この問題を解いてみる →

パターン5

 1  type Setting = { key: string };
 2  const raw = { key: "vol", value: 10 };
 3  const setting: Setting = raw;         
                       ^
 4  console.log(setting.value);
main.ts(4,21): error TS2339: Property 'value' does not exist on type 'Setting'.

: Settingという型注釈はsettingの型をSettingに絞り込みます。satisfiesなら元の型のままvalueにアクセスできます。

直し方: const setting: Setting = raw; を const setting = raw satisfies Setting; にします。

この問題を解いてみる →

よくある誤解

「型注釈を付けると、余分なプロパティは実際に消される」という考えは誤りです。消えるのは型情報の上での見え方だけで、実行時のオブジェクトには元のプロパティがそのまま残っています。

実務での勘所

satisfiesが追加される前は、: Typeという型注釈を使うと、その型に合っているかの検証は受けられる代わりに、実際の値が持っていたより詳しい型情報が失われる、という二者択一でした。TS 4.9で追加されたsatisfiesは、Typeに合致しているかの検証はしっかり行いながら、変数自体の型は元の値から推論された、より詳しい型のまま保持するという、両方の利点を同時に得るために設計されました。

演習をはじめる

関連するエラー

広告
広告スロット(未設定)