How to create a circularly referenced type in TypeScript?

前端 未结 4 1337
生来不讨喜
生来不讨喜 2020-12-05 13:04

I have the following code:

type Document = number | string | Array;

TypeScript complains with the following error:

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 13:30

    Building on what NPE said, types cannot recursively point to themselves, you could unroll this type to whatever level of depth you considered sufficient, e.g.:

    type Document = [number|string|[number|string|[number|string|[number|string]]]]
    

    Not pretty, but removes the need for an interface or class with a property value.

提交回复
热议问题