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
- Fix a regression - see Vincit/objection.js#2178 — committed to myrotvorets/psb-api-identigraf-auth by myrotvorets-team 3 years ago
- Fix a regression - see Vincit/objection.js#2178 — committed to myrotvorets/psb-api-identigraf-decoder by myrotvorets-team 3 years ago
- attempt to fix a typescript performance/infinite recursion problem. See #2177, #2178 and #2132 — committed to Vincit/objection.js by koskimas 3 years ago
Another way to reproduce it is to upgrade the
examples/koa-ts
repo to TypeScript4.5.3
(oddly4.5.2
works in this case) and add one of the following totsconfig.json
:or
Compiling the project will reproduce the error:
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 TypeScript4.5.x
package.json:
index.ts:
Run:
I should note that splitting
ModelA
andModelB
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 totrue
. I definitely want to usestrictNullChecks
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 theMaximum call stack size exceeded
error ints-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 onlyts-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.
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.
https://github.com/hkjeffchan/obj3rangeError
change typescript to “<4.5.3” runs perfectly
"typescript": "<4.5.3"
works.setting “strictNullChecks”: false can fix temporarily
Linking my fix without downgrading TS: https://github.com/Vincit/objection.js/issues/2132#issuecomment-1001968056