TypeScript
TypeScript copied to clipboard
More Recursive Type Declarations
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.
please see https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
I definitely never saw that comment. My apologies.