Typescript Definitely assigned fields problem when defining models
See original GitHub issueFollowing the typescript example, I created the following class:
import { Model } from '@nozbe/watermelondb'
import { field, children, date } from '@nozbe/watermelondb/decorators'
export default class Farm extends Model {
static table = 'farms'
static associations = {
user: { type: 'has_many', foreignKey: 'user_id' },
cattle: { type: 'has_many', foreignKey: 'cattle_id' },
} as const
@date('created_at') createdAt!: string
@date('updated_at') updatedAt!: string
@field('name') name!: string
@field('car_number') carNumber!: string
@field('car_data') carData!: string
@children('users') users!: string
@children('cattle') cattle!: string
}
When I save my file, TS throws the following error:
error: src/services/adapter/watermelonDB/models/Farm.ts: /home/angelo/Documentos/GIT/professional/databoi/databoi-front/src/services/adapter/watermelonDB/models/Farm.ts: Definitely assigned fields cannot be initialized here, but only in the constructor
10 | } as const
11 |
> 12 | @date('created_at') createdAt!: string
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13 | @date('updated_at') updatedAt!: string
14 |
15 | @field('name') name!: string
Turning off strictPropertyInitialization
in tsconfig
is not acceptable (I’d need to turn strict
off and that defeats the purpose of strict typechecking).
Defining the variables inside a constructor, like below, gives me ExceptionsManager.js:179 Diagnostic error: Diagnostic error: Not allowed to change record outside of create/update()
error.
import { Model } from '@nozbe/watermelondb'
import { field, children, date } from '@nozbe/watermelondb/decorators'
export default class Farm extends Model {
constructor() {
super()
this.createdAt = ''
this.updatedAt = ''
this.name = ''
this.carNumber = ''
this.carData = ''
this.users = ''
this.cattle = ''
}
static table = 'farms'
static associations = {
user: { type: 'has_many', foreignKey: 'user_id' },
cattle: { type: 'has_many', foreignKey: 'cattle_id' },
} as const
@date('created_at') createdAt
@date('updated_at') updatedAt
@field('name') name
@field('car_number') carNumber
@field('car_data') carData
@children('users') users
@children('cattle') cattle
}
How can I solve this problem?
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
TypeScript complain "has no initializer and is not definitely ...
TypeScript complain "has no initializer and is not definitely assigned in the constructor" about constructors by returning constructed object.
Read more >Strict Property Initialization in TypeScript - Marius Schulz
The reason for the runtime error is that the username property holds the value undefined because there's no assignment to that property.
Read more >TSConfig Reference - Docs on every TSConfig option
From allowJs to useDefineForClassFields the TSConfig reference includes information about all of the active compiler flags setting up a TypeScript project.
Read more >Known Issues - typegoose
Using babel as a TypeScript compiler is known to cause problems (like incorrect types) (see transpile-only ), it is recommended you use tsc...
Read more >Property has no initializer and is not definitely assigned
If you have ever seen an error that looks like Property 'X' has no initializer and is not definitely assigned in the constructor....
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
hey @angelod1as , I was also facing this issue and this is what helped me.
The issue stems from the different @babel plugins and you would need to add a few and no need for a constructor.
Install the following babel plugins and add them under plugins in your babel.config.js file:
@angelod1as do it