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 error with documentation example

See original GitHub issue

When I write the following code, straight from docs:

class Post extends Model {
  static table = 'posts'
  static associations = {
    comments: { type: 'has_many', foreignKey: 'post_id' },
  }
}

Typescript complains:

Class static side 'typeof Post' incorrectly extends base class static side 'typeof Model'.
  Types of property 'associations' are incompatible.
    Type '{ comments: { type: string; foreignKey: string; }; }' is not assignable to type 'Associations'.
      Property 'comments' is incompatible with index signature.
        Type '{ type: string; foreignKey: string; }' is not assignable to type 'AssociationInfo'.
          Type '{ type: string; foreignKey: string; }' is not assignable to type 'HasManyAssociation'.
            Types of property 'type' are incompatible.
              Type 'string' is not assignable to type '"has_many"'.

Is there a way to solve this?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

19reactions
angelod1ascommented, Jan 7, 2021

The solutions was declare associations as const

class Post extends Model {
  static table = 'posts'
  static associations = {
    comments: { type: 'has_many', foreignKey: 'post_id' },
  } as const  // <- here
} 
4reactions
angelod1ascommented, Jan 7, 2021

I’ll try to elaborate, as I don’t know exactly what’s going wrong myself.

AFAIK, the error complains that the types don’t match, specially because the object property (comments) and the information in type (has_many) and foreignKey (post_id) aren’t found within the typeseting of Post or Model type. There’s an issue here that I’ll address later on this text.

What as const does it: instead of saying “type is receiving a value type string”, it turns the strings into constants. So it says: “type is receiving a value type has_many”. As I say the whole object is as const, the program understands that all the object is a constant and valid.

If anyone could elaborate this better (and even maybe say I got all this wrong), I’d be happy.

The main issue (highlighted above) is that I believe the typesetting of Watermelon isn’t very good. It throws errors here and there and the typescript documentation is a single .ts file without real implementation. I do not have enough knowledge to PR a new example, so all I can do is file issues 😢

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Understanding Errors - TypeScript
Let's work through some examples to see how they work in practice. Here's an example that produces an error message longer than the...
Read more >
TypeScript errors and how to fix them
A list of common TypeScript errors and how to fix them.
Read more >
Complete list of Typescript error codes and their fixes
In Typescript, where do I find a complete reference of all error codes and their fixes. My usecase is that I often see...
Read more >
Error handling in TypeScript like a pro - Journal - Plain
On our API we would translate these domain errors to response errors. For example in the following handleApiCall function could then handle each ......
Read more >
Error - JavaScript - MDN Web Docs - Mozilla
Examples · Throwing a generic error · Handling a specific error type · Differentiate between similar errors · Custom error types.
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