objection.js: RangeError: Maximum call stack size exceeded at recursiveTypeRelatedTo after upgrading to 3.0

import { Model, RelationMappings, RelationMappingsThunk } from 'objection';

export class BaseModel extends Model {
  id: number;

  static get modelPaths() {
    return [__dirname];
  }
}

export class Product extends BaseModel {
  static tableName = 'product';
  price: number;
  user?: User;

  static relationMappings: RelationMappings | RelationMappingsThunk = {
    productList: {
      relation: Model.BelongsToOneRelation,
      modelClass: 'User',
      join: {
        from: 'product.userId',
        to: 'user.id',
      },
    },
  };
}

export class User extends BaseModel {
  static tableName = 'user';
  name: string;
  // productList?: Product;

  static relationMappings: RelationMappings | RelationMappingsThunk = {
    productList: {
      relation: Model.HasManyRelation,
      modelClass: 'Product',
      join: {
        from: 'user.id',
        to: 'product.userId',
      },
    },
  };
}

If I uncomment the line “productList?: Product”, I got the following error: RangeError: Maximum call stack size exceeded at recursiveTypeRelatedTo (/Applications/MAMP/htdocs/test-obj3/node_modules/typescript/lib/typescript.js:63793:44) at isRelatedTo (/Applications/MAMP/htdocs/test-obj3/node_modules/typescript/lib/typescript.js:63388:30) at typeRelatedToSomeType (/Applications/MAMP/htdocs/test-obj3/node_modules/typescript/lib/typescript.js:63654:35) at structuredTypeRelatedToWorker (/Applications/MAMP/htdocs/test-obj3/node_modules/typescript/lib/typescript.js:63926:32) at structuredTypeRelatedTo (/Applications/MAMP/htdocs/test-obj3/node_modules/typescript/lib/typescript.js:63908:30) at isRelatedTo (/Applications/MAMP/htdocs/test-obj3/node_modules/typescript/lib/typescript.js:63385:30) at checkTypeRelatedTo (/Applications/MAMP/htdocs/test-obj3/node_modules/typescript/lib/typescript.js:63003:26) at isTypeRelatedTo (/Applications/MAMP/htdocs/test-obj3/node_modules/typescript/lib/typescript.js:62954:24) at isTypeAssignableTo (/Applications/MAMP/htdocs/test-obj3/node_modules/typescript/lib/typescript.js:62160:20) at getConditionalType (/Applications/MAMP/htdocs/test-obj3/node_modules/typescript/lib/typescript.js:60943:133)

This only happens after upgrading to objection@3.0.0. This is fine in 2.x.

Already set tsconfig to “strict”: true,

Seems related to https://github.com/microsoft/TypeScript/issues/33460

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 14
  • Comments: 25 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Another way to reproduce it is to upgrade the examples/koa-ts repo to TypeScript 4.5.3 (oddly 4.5.2 works in this case) and add one of the following to tsconfig.json:

{
  "compilerOptions": {
    "skipLibCheck": true
  }
}

or

{
  "compilerOptions": {
    "strictFunctionTypes": false
  }
}

Compiling the project will reproduce the error:

> npx tsc

RangeError: Maximum call stack size exceeded

I suspect because the error is fixed by downgrading to TypeScript 4.5.2 in this case that there might be several different issues causing the same error.

Here’s a simplified reproduction. Tested with Objection 3.0.1 and TypeScript 4.5.x

package.json:

{
  "devDependencies": {
    "typescript": "4.5.2"
  },
  "dependencies": {
    "knex": "^1.0.3",
    "objection": "^3.0.1"
  }
}

index.ts:

import { Model } from 'objection';

class ModelA extends Model {
  id!: number;
}

class ModelB extends Model {
  id!: number;
  static relationMappings = {
    model: {
      relation: Model.HasManyRelation,
      modelClass: ModelA,
      join: { to: "", from: "" },
    },
  };
}

Run:

> npm install
> tsc --strictNullChecks index.ts

RangeError: Maximum call stack size exceeded

I should note that splitting ModelA and ModelB into separate files and using require, e.g.: modelClass: require('./ModelA'), fixes the error.

I was not able to fix the error by downgrading TypeScript, so it might be a different issue from what others are describing.

3.0.1 + TS 4.5.4: Still RangeError: Maximum call stack 3.0.0 + TS 4.5.2: No error 3.0.1 + TS 4.5.2: No error

I can confirm this happens on latest versions of Objection and TypeScript when strictNullChecks is set to true. I definitely want to use strictNullChecks though so I hope this will be fixed soon.

Note: We have strict mode running for all of the below.

Initial problem

Objection.js: v3.0.1 TS: v4.5.4

We’re also having this issue. I can’t provide a repo with the error, but I can talk through what I was seeing which will hopefully help debugging. But it’s very strange as the repo has no TypeScript errors and none of the other issues people in this thread have referenced.

In the following code sample, uncommenting the doSomething function caused the Maximum call stack size exceeded error in ts-node. Nothing is calling that function. The name of the function didn’t impact anything (neither did changing the return type and method body). yarn tsc --noEmit also succeeds in both cases (commented or uncommented). So only ts-node is failing when uncommented.

Commenting it out again made ts-node run without a problem. We have a very vanilla use of Objection.js so far.

export class AnswerModel extends BaseModel {
	static tableName = 'answers'

	// Default
	id?: string
	text?: string
	imageUrl?: string

	// doSomething() {
	// 	console.log('Test')
	// }
}

Attempted solution 1

Objection.js: v3.0.1 TS: v4.5.2

We downgraded the TS version as some had recommended. And it fixed the error in ts-node. We kept developing for another week and the error popped up again (again, unrelated to any type errors).

Attempted solution 2

Objection.js: v2.2.18 (latest 2.x version) TS: v4.5.2

We downgraded Objection (and kept the downgraded TS) and the error seems to have gone away again. But we’d love to be on 3.0.x. I will update this comment if we see it again.

Doesn’t generate the error when I run it. By a reproduction I mean the whole project, tsconfig and everything. @koskimas

https://github.com/hkjeffchan/obj3rangeError

change typescript to “<4.5.3” runs perfectly

"typescript": "<4.5.3" works.

setting “strictNullChecks”: false can fix temporarily