Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

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

Changed type of return value of Model<TInstance, TAttributes>.scope #14

Merged
merged 2 commits into from
Jul 17, 2016

Conversation

Thylossus
Copy link
Contributor

Changed type of return value of Model<TInstance, TAttributes>.scope from this to Model<TInstance, TAttributes>. The following example in https://github.com/Thylossus/sequelize-pull-request-justification-demo/tree/master tries to mimic the example from https://github.com/louy/typed-sequelize/blob/master/README.md.

However, I cannot compile this example due to a type error:

index.ts(16,16): error TS2322: Type 'sequelize.Model<Thing, ThingInstance>' is not assignable to type 'sequelize.SequelizeStaticAndInstance.Model<Thing, ThingInstance>'.
  Types of property 'scope' are incompatible.
    Type '(options?: string | string[] | ScopeOptions | WhereOptions) => Model<Thing, ThingInstance>' is not assignable to type '(options?: string | string[] | ScopeOptions | WhereOptions) => this'.
      Type 'Model<Thing, ThingInstance>' is not assignable to type 'this'.

This is probably related to microsoft/TypeScript#5863. Changing the return type of scope solves the error for me.
Of course, this is not a sufficient criterion for changing your code, but I believe that isn't working for anyone. Feedback about the correctness of this assumption is very welcome.

Thank you for providing the typings!

Changed type of return value of `Model<TInstance, TAttributes>.scope` from `this` to `Model<TInstance, TAttributes>`. The following example in https://github.com/Thylossus/sequelize-pull-request-justification-demo/tree/master tries to mimic the example from https://github.com/louy/typed-sequelize/blob/master/README.md.

However, I cannot compile this example due to a type error:

```
index.ts(16,16): error TS2322: Type 'sequelize.Model<Thing, ThingInstance>' is not assignable to type 'sequelize.SequelizeStaticAndInstance.Model<Thing, ThingInstance>'.
  Types of property 'scope' are incompatible.
    Type '(options?: string | string[] | ScopeOptions | WhereOptions) => Model<Thing, ThingInstance>' is not assignable to type '(options?: string | string[] | ScopeOptions | WhereOptions) => this'.
      Type 'Model<Thing, ThingInstance>' is not assignable to type 'this'.
```

This is probably related to microsoft/TypeScript#5863. Changing the return type of `scope` solves the error for me.
Of course, this is not a sufficient criterion for changing your code, but I believe that isn't working for anyone. Feedback about the correctness of this assumption is very welcome.

Thank you for providing the typings!
@felixfbecker
Copy link
Collaborator

Your type arguments are switched, it's sequelize.define<TInstance, TAttributes>

@louy
Copy link
Collaborator

louy commented Jul 6, 2016

This is a typo in the readme file. it should be Model<ThingInstance, Thing>. I'll update it.

@louy louy closed this in 283237d Jul 6, 2016
@felixfbecker
Copy link
Collaborator

But that doesn't change the error though. It can't be related to the issue you linked as scope() is not static. I actually think this is a bug in TypeScript, it doesn't get that this in that case is equivalent to Model<ThingInstance, Thing>... But I think changing the return type will not do any harm.

@felixfbecker
Copy link
Collaborator

@louy Reopening because that doesn't solve this issue ;)

@felixfbecker felixfbecker reopened this Jul 6, 2016
@Thylossus
Copy link
Contributor Author

Thank you for your responses. I've updated my code in https://github.com/Thylossus/sequelize-pull-request-justification-demo/tree/master. Nonetheless, the issue remains. In accordance with https://github.com/louy/typed-sequelize/blob/master/index.d.ts#L5296, https://github.com/louy/typed-sequelize/blob/master/index.d.ts#L2578, and https://github.com/louy/typed-sequelize/blob/master/index.d.ts#L3468, I swapped Thing and ThingInstance for Instance, Model, and define.

The error is now

index.ts(19,16): error TS2322: Type 'sequelize.Model<ThingInstance, Thing>' is not assignable to type 'sequelize.SequelizeStaticAndInstance.Model<ThingInstance, Thing>'.
  Types of property 'scope' are incompatible.
    Type '(options?: string | string[] | ScopeOptions | WhereOptions) => Model<ThingInstance, Thing>' is not assignable to type '(options?: string | string[] | ScopeOptions | WhereOptions) => this'.
      Type 'Model<ThingInstance, Thing>' is not assignable to type 'this'.

(basically the same error with Thing and ThingInstance swapped).

@louy
Copy link
Collaborator

louy commented Jul 6, 2016

Right @felixfbecker.
Thanks for taking the time to demonstrate this @Thylossus.

So for some reason the return type of Connection#define is not of type Sequelize.Model.

I'll take a look at it later today. It might be better to have this as an issue rather than a PR as this clearly doesn't fix it.

@Thylossus
Copy link
Contributor Author

@felixfbecker Oh, you are right, of course. scope isn't static. It is probably rather related to microsoft/TypeScript#7818 and/or microsoft/TypeScript#6223 and thus should be fixed in TypeScript version 2.0 (its part of the 2.0 milestone).

@Thylossus
Copy link
Contributor Author

@louy Okay, so should I close this PR and open an issue referencing this PR?

@louy louy merged commit 39b5073 into types:master Jul 17, 2016
louy added a commit that referenced this pull request Jul 17, 2016
@Thylossus Thylossus deleted the patch-1 branch July 18, 2016 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants