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: property data type does not always need to match the usage interface

See original GitHub issue

Issue Description

Getters this.getDataValue returns the type declared on the class property and not on the Column.type.

Additional context

class Account extends Model<Account> {
@AllowNull
  @Column({
    type: DataType.DATEONLY, // SHOULD BE WHATEVER THIS IS
    get(this: Account): moment.Moment | null {
      const date = this.getDataValue('date_discharged'); // TYPESCRIPT SAYS THIS IS MOMENT | UNDEFINED
      return getDate(date);
    },
    set(this: Account, val: moment.Moment | null) {
      this.setDataValue('date_discharged', val == null ? null : val.format());
    }
  })
  date_discharged?: moment.Moment; // BECAUSE OF THIS, I DO THIS TO USE MOMENT INSTEAD OF DATES.
}

Author of sequelize-typescript said the following

Hey @spearmootz thanks for reporting. Unfortunately this cannot be fixed within sequelize-typescript, but need to be solved on sequelize side. The actual issue is the type of getDataValue: https://github.com/sequelize/sequelize/blob/master/types/lib/model.d.ts#L2551. As your example makes it very clear, the internal type (the type stored in dataValues) doesn’t necessarily need to be the type of the property type. Can you open an issue in the sequelize repo, please?

Issue Template Checklist

Is this issue dialect-specific?

  • No. This issue is relevant to Sequelize as a whole.
  • Yes. This issue only applies to the following dialect(s): XXX, YYY, ZZZ
  • I don’t know.

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don’t know how to start, I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

I have no clue how to achieve the right type using typescript.

Although in the models tab, every time there is mention of this[k] it is wrong, but rather it should be of the this[k].underlyingType.

i am going to submit the ticket and simultaneously attempt to fix this.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
papbcommented, Oct 30, 2019

Thanks for the report. I edited your posts to add some formatting, please learn to do that in the future 😃

1reaction
yoshigcommented, Jul 11, 2022

Any updates on this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handbook - Interfaces - TypeScript
Not all properties of an interface may be required. ... For function types to correctly type check, the names of the parameters do...
Read more >
Why won't TypeScript compiler complain about objects that do ...
If I send an object that does not exactly match the shape (an addition key for example) by storing it in a variable,...
Read more >
Understanding and using interfaces in TypeScript
Types of variables are not predictable in JavaScript. ... it is not necessary to have all of the properties as defined in the...
Read more >
A Simple Guide to Typescript Interfaces: declaration & use cases
In TypeScript, an interface is an abstract type that tells the compiler which property names a given object can have.
Read more >
A simple guide to “interface” data type in TypeScript - Medium
When we define an object with properties (keys) and values, TypeScript creates an implicit interface by looking at the property names and data...
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