objection.js: A problem with typescript version 4

Hi, objection.js is a great tool. I am using latest version of knex and typescript, so want to use objection@next but it is throwing error:

           throw e;
                ^
RangeError: Maximum call stack size exceeded
    at compareSignaturesRelated (\node_modules\typescript\lib\tsc.js:50982:27)
    at signatureRelatedTo (\node_modules\typescript\lib\tsc.js:52833:24)
    at signaturesRelatedTo (\node_modules\typescript\lib\tsc.js:52776:39)
    at structuredTypeRelatedToWorker (\node_modules\typescript\lib\tsc.js:52309:39)
    at structuredTypeRelatedTo (\node_modules\typescript\lib\tsc.js:51954:30)
    at recursiveTypeRelatedTo (\node_modules\typescript\lib\tsc.js:51930:53)
    at isRelatedTo (\node_modules\typescript\lib\tsc.js:51527:34)
    at isPropertySymbolTypeRelated (\node_modules\typescript\lib\tsc.js:52488:24)
    at propertyRelatedTo (\node_modules\typescript\lib\tsc.js:52520:31)
    at propertiesRelatedTo (\node_modules\typescript\lib\tsc.js:52708:43)

package.json:

        "objection": "^3.0.0-alpha.5",
        "typescript": "^4.4.4"

Which version of objection.js and typescript should I use?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 6
  • Comments: 25

Commits related to this issue

Most upvoted comments

Hi! Same as @Linksku, I also need to wrap the declaration of PartialModelGraph with T extends any ? ... : never. If I don’t do this, the types of “nested” models (from relationMappings) in a model graph are not inferred by TypeScript. However, I do not face a Maximum call stack size exceeded error when compiling my project without this fix:

type PartialModelGraph<M, T = M & GraphParameters> = T extends any ? {
    [K in DataPropertyNames<T>]?: Defined<T[K]> extends Model
      ? PartialModelGraph<Defined<T[K]>>
      : Defined<T[K]> extends Array<infer I>
      ? I extends Model
        ? PartialModelGraph<I>[]
        : Expression<T[K]>
      : Expression<T[K]>;
  } : never;

@koskimas: Cloud you release a new version with the T extends any ? ... : never fix? I would be very grateful! 😃

FYI: It does not matter if I set strict: true or strict: false in my tsconfig.json file - in both cases, I still need the T extends any ? ... : never fix.

Package versions:

typescript: 4.5.4
objection: 3.0.1
knex: 0.95.15

I’m running into this issue with the released version of 3.0.0

@koskimas are you saying that objection 3 requires strict: true to work properly with typescript? Is that documented anywhere? It seems like a pretty significant limitation.

Hi, I have the same issue. Also fixed with the T extends any ? ... : never fix. Please can you release a new version with this included?

I am getting the same error on Typescript 4.5.5 as well. I was able to get rid of the problem by setting strictNullChecks: false in tsconfig.json as mentioned in related issue but it’s just a temporary fix.

@koskimas I think the issue is caused by NonFunctionPropertyNames including QueryBuilderType. It should go away by excluding QueryBuilderType in PartialModelObject and PartialModelGraph:

  type PartialModelObject<T extends Model> = {
    [K in Exclude<NonFunctionPropertyNames<T>, 'QueryBuilderType'>]?: Defined<T[K]> extends Model
      ? T[K]
      : Defined<T[K]> extends Array<infer I>
      ? I extends Model
        ? I[]
        : Expression<T[K]>
      : Expression<T[K]>;
  };

I’m not sure why I also needed T extends any for PartialModelGraph, probably some weird union issue:

  type PartialModelGraph<M, T = M & GraphParameters> = T extends any ? {
    [K in Exclude<NonFunctionPropertyNames<T>, 'QueryBuilderType'>]?: Defined<T[K]> extends Model
      ? PartialModelGraph<Defined<T[K]>>
      : Defined<T[K]> extends Array<infer I>
      ? I extends Model
        ? PartialModelGraph<I>[]
        : Expression<T[K]>
      : Expression<T[K]>;
  } : never;

I don’t think strict is required, but it will point out any errors in your model files. In my case the solution was to properly define the properties with the ! operator, e.g. id!: string

Edit: Actually, strict is required for compilation.