現象
typeをimplementsしようとすると、以下のエラーが出た。
A class can only implement an object type or intersection of object types with statically known members.
原因
どうやらUnionTypeはimplementsできないらしい。
サンプル
type Human = { head: any; hands: any legs: any; } type Hourse = { legs: any tail: any } type Therianthrope = Human | Hourse; class Kentauros implements Therianthrope { head: any; hands: any legs: any; tail: any }
playgroundで試した結果↓
TypeScript: TS Playground - An online editor for exploring TypeScript and JavaScript
解決策
interfaceのextendsができる。
interface Human { head: any; hands: any legs: any; } interface Hourse { legs: any tail: any } interface Therianthrope extends Human, Hourse {} class Kentauros implements Therianthrope { head: any; hands: any legs: any; }
TypeScript: TS Playground - An online editor for exploring TypeScript and JavaScript