Skip to content

Sum Type

型を合成した総称。Tagged Unionとも呼ばれる。

type EC2 = Readonly<{
type: string;
name: string;
}>;
type S3 = Readonly<{
type: string;
region: string
}>;
type Service = EC2 | S3;

GoではSealed InterfaceでSum Typeを実装する。

type Service interface {
privateService()
}
type EC2 struct {}
func (*EC2) privateService() {}
type S3 struct {}
func (*S3) privateService() {}

このとき、switch はTypeScriptほど自然にはできないので、網羅したければGoでinterfaceのtype switchに記述漏れがあったら検出するような仕組みが必要になる。