NISHIO Hirokazu[Translate]
TypeScriptは型を再帰的に定義できない?

まとめ
interfaceのような名前のついた型ではツリーの定義などは前からできた
3.7からtype aliasでも一部のケースで出来るようになった
しかし今回やりたかったような方法での再起的定義はできない
それをinterfaceにすることもできない

足し算したかっただけなんだけど…
ts
type N0 = []; type succ<N> = [any, N]; type N1 = succ<N0> type N2 = succ<N1> type N3 = succ<N2> type pred<N> = N extends [any, infer R] ? R : never; type NEQUAL<Na, Nb> = Na extends Nb ? any : never; type testNEQUAL = NEQUAL<pred<N2>, succ<N0>> // any type add<Na, Nb> = Nb extends N0 ? Na : add<succ<Na>, pred<Nb>> // Type alias 'add' circularly references itself.

TypeScript 3.7で
ts
type ValueOrArray<T> = T | Array<ValueOrArray<T>>; // OK type ValueOrArray<T> = T | ValueOrArray<Array<T>>; // NG

ts
type add<Na, Nb> = Nb extends N0 ? Na : add2<Na, Nb>; interface add2<Na, Nb> extends add<succ<Na>, pred<Nb>> { }; // NG // An interface can only extend an object type or intersection of object types with statically known members.

"Engineer's way of creating knowledge" the English version of my book is now available on [Engineer's way of creating knowledge]

(C)NISHIO Hirokazu / Converted from [Scrapbox] at [Edit]