How to create a circularly referenced type in TypeScript?

The creator of TypeScript explains how to create recursive types here.

The workaround for the circular reference is to use extends Array. In your case this would lead to this solution:

type Document = number | string | DocumentArray;

interface DocumentArray extends Array<Document> { }

Update (TypeScript 3.7)

Starting with TypeScript 3.7, recursive type aliases will be permitted and the workaround will no longer be needed. See: https://github.com/microsoft/TypeScript/pull/33050

Leave a Comment