I've got two type definitions describing almost the same thing:
// compilation fails with `Type alias 'StringOrRecordOfStrings' circularly references itself.ts(2456)`
type StringOrRecordOfStrings = string | string[] | Record<string, StringOrRecordOfStrings>;
// compiles without problems
type StringOrRecordOfStrings = string | string[] | { [property: string]: StringOrRecordOfStrings };
Is anyone able to explain why the first type definition doesn't compile?
- The recursive type aliases have been introduced in version 3.7 https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#more-recursive-type-aliases
- This SO answer explains how to use recursive type aliases Recursive Types in TypeScript
- for brevity I omitted the remaining key types from record, but it works even when we set the
[property: string | number | symbol]
.