TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

More Recursive Type Declarations

Open opensrcken opened this issue 10 years ago • 2 comments

TypeScript Version:

nightly

Code

// A self-contained demonstration of the problem follows...
type Json = string | number | boolean | null | JsonObject | JsonArray;
type JsonObject = { [key:string]: Json };
type JsonArray = Array<Json>;

Expected behavior: Compiles.

Actual behavior: Type alias 'Json' circularly references itself. Type alias 'JsonArray' circularly references itself.

As a workaround, I'm using the more verbose:

declare type Json = JsonObject | number | string | JsonArray;
declare type JsonArray = IMaybeRecursiveArray<JsonObject | number | string>;
interface IMaybeRecursiveArray<T> {
  [i: number]: T | IMaybeRecursiveArray<T>;
}
interface JsonObject {
  [paramKey: string]: Json
}

But FlowType can compile the more "direct" type declarations. It would be nice if Typescript could support this as well.

opensrcken avatar Jun 10 '16 12:06 opensrcken

please see https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540

mhegazy avatar Jun 10 '16 17:06 mhegazy

I definitely never saw that comment. My apologies.

opensrcken avatar Jun 10 '16 23:06 opensrcken