Typescript error with documentation example
See original GitHub issueWhen 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:
- Created 3 years ago
- Comments:5
Top 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 >
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

The solutions was declare
associationsas constI’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 intype(has_many) andforeignKey(post_id) aren’t found within the typeseting ofPostorModeltype. There’s an issue here that I’ll address later on this text.What
as constdoes it: instead of saying “typeis receiving a value typestring”, it turns the strings into constants. So it says: “typeis receiving a value typehas_many”. As I say the whole object isas 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
.tsfile without real implementation. I do not have enough knowledge to PR a new example, so all I can do is file issues 😢