question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript Definitely assigned fields problem when defining models

See original GitHub issue

Following 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:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
avinashlng1080commented, Jan 8, 2021

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:

  plugins: [
        ['@babel/plugin-proposal-decorators', {legacy: true}],
        ['@babel/plugin-transform-flow-strip-types'],
        ['@babel/plugin-proposal-class-properties', {loose: true}],
        // add other plugins afterwards ....
]
0reactions
radexcommented, Jan 13, 2021
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found