0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TypeScriptで万能型変換関数を実装しました

0
Last updated at Posted at 2025-12-12

はじめに

TypeScriptの構造的部分型のトラップを明確にするため、無闇なスプレッド構文の使用が型システムの健全性を脅かし得ることを、万能型変換関数を作成することで示します。

本記事のコードは MIT License とします。

万能型変換関数

任意の型T, SについてT型の値をS型に変換します。
変換を実行するためには、S型の値の存在証明が必要なため証人を一つ与える必要があります。

const convert = <T, S>(value: T, witness: S): S => {
  const obj1 = { a: witness, b: null } satisfies { a: S, b: null };
  const obj2 = { a: value, b: null } satisfies { a: T, b: null };
  const f = (x: { a: T, b: null }): { b: null } => x;
  const obj = { ...obj1, ...f(obj2) } satisfies { a: S };
  return obj.a;
};

使い方

文字列 "str"number 型に変換するには次のようにします。
number 型の証人としては 0 を与えます。

const n = convert("str", 0) satisfies number;
console.log(n satisfies number); // => "str"
console.log(typeof (n satisfies number)); // => string

実行時には string が返りますが、TypeScript の型システム上は number として扱われます。

おわりに

TypeScriptの構造的部分型とスプレッド構文を組み合わせることによって、万能型変換関数が作れてしまうことを示しました。意図せずに実体と異なる型を付けてしまわないように気をつけてTypeScriptを使いましょう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?