TypeScriptで Haskellのような newtype を実現できているかもしれない方法
code:ts
// Nameはstringのnewtype
type Name = string & { readonly phantom: unique symbol };
// Idはstringのnewtype
type Id = string & {readonly phantom: unique symbol};
function wrap<T, S extends T & {readonly phantom: Symbol}>(v: T): S {
return v as S;
}
// stringをラップしてName型にする
const name1: Name = wrap("john");
// Name型をstringとしてそのまま扱える (by @Linda_pp thanks!)
const nameStr1: string = name1;
// NameとIdはstring型ではなくちゃんと実行前に検出できる
// const id1: Id = name1;