Cannot access [Model] before initialization
See original GitHub issueVersions
- sequelize: 4.44.4
- sequelize-typescript: 1.1.0
- typescript: 3.9.2
I’m submitting a …
[X] bug report [ ] feature request
Actual behavior:
$ ./node_modules/.bin/ts-node test.ts ReferenceError: Cannot access ‘Team’ before initialization
Expected behavior:
No error.
Steps to reproduce:
Use (basically) the code from the README (https://github.com/RobinBuschmann/sequelize-typescript#one-to-many) as test.ts:
import {BelongsTo, Column, ForeignKey, HasMany, Model, Table} from "sequelize-typescript";
@Table
export class Player extends Model<Player> {
@Column
name!: string;
@Column
num!: number;
@ForeignKey(() => Team)
@Column
teamId!: number;
@BelongsTo(() => Team)
team!: Team;
}
@Table
export class Team extends Model<Team> {
@Column
name!: string;
@HasMany(() => Player)
players!: Player[];
}
tsconfig.json:
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"lib": ["es2019", "es2020.promise", "es2020.bigint", "es2020.string"],
"noImplicitAny": true,
"module": "commonjs",
"outDir": "_dist",
"rootDir": ".",
"skipLibCheck": true,
"sourceMap": false,
"strict": true,
"strictNullChecks": true,
"target": "ES2019"
}
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:18
Top Results From Across the Web
ReferenceError: can't access lexical declaration 'X' before ...
The JavaScript exception "can't access lexical declaration `variable' before initialization" occurs when a lexical variable was accessed ...
Read more >Cannot access 'variable_name' before initialization
When you assign variables using $: you cannot assign them as part of other variables declared using let , const , or var...
Read more >ReferenceError: Cannot access before initialization in JS
The "Cannot access before initialization" error occurs when a variable declared using let or const is accessed before it was initialized in the...
Read more >Cannot access [Model] before initialization · Issue #825 - GitHub
I resolved that just putting each model in different files. It is not a sequelize issue, seems like the problem is on typescript...
Read more >ReferenceError: Cannot access 'user' before initialization
I have this error that I can't really figure out why I am getting. I use mongoose to model my db models, and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve got another workaround.
I’ve noticed that it is possible to reference classes declared later when defining arrays
Which brought me to this:
Compiles without errors.
The root cause of all these issues is the requirement of recursive imports. This PR (https://github.com/RobinBuschmann/sequelize-typescript/pull/1206) solve that in part. But in order to completely solve this, I think the returned reference from an association should be a generic model based on the interface.
So, the final solution could look something like this: