deepkit-framework icon indicating copy to clipboard operation
deepkit-framework copied to clipboard

type-compiler breaks on complex types

Open wielski opened this issue 3 years ago • 0 comments

type-compiler could not serialize this kind of legit typescript types:

export type Option<T> = OptionType<T>;
class OptionType<T> {
  constructor(val: T, some: boolean) {}
}

export function Option<T>(val: T): Option<T> {
  return new OptionType(val, true);
};

const value: Option<string> = Option('hello');

In that case Option is both type alias to OptionType class, and a function that calls for new OptionType. It compiles successfully with default tsc, but type-compiler breaks this functionality.

RangeError: Maximum call stack size exceeded
    at findVariable (deepkit-example/node_modules/@deepkit/type-compiler/dist/cjs/src/compiler.js:119:22)
    at findVariable (deepkit-example/node_modules/@deepkit/type-compiler/dist/cjs/src/compiler.js:125:16)
    at findVariable (deepkit-example/node_modules/@deepkit/type-compiler/dist/cjs/src/compiler.js:125:16)

This happens because of type-compiler loops between FunctionDeclaration and TypeReference. One of possible solution is to add some nodes memorizing, and limit maximum depth for iterating over same nodes, as TypeScript does.

wielski avatar Aug 18 '22 11:08 wielski