添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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?

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
  josephsintum, LukasMod, sevvaleygul0, upendra-web, sou-gabriel, PauleBertt, tiaanduplessis, scob7, Podmenato, Dheph, and 18 more reacted with thumbs up emoji
  Dheph, ebnersilva, joonaaas, pedrio-gh, MrLightful, and heliocosta-dev reacted with hooray emoji
  pedrorambo, sevvaleygul0, upendra-web, prof-xed, and heliocosta-dev reacted with eyes emoji
    All reactions
          

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 😢

It's because of literal types (eg. 'belongs_to' or 'has_many') being 'widened' to type string for field type
You can also use the new 'const' assertion from typescript 3.4

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