sequelize / sequelize-typescript

Decorators and some other features for sequelize
MIT License
2.78k stars 280 forks source link

Can not use sequelize-typescript Models with sequelize ModelsHashInterface #200

Closed chanlito closed 5 years ago

chanlito commented 6 years ago

Hello everyone, currently sequelize instance has this models field which is of type ModelsHashInterface.

Is there a better way than using (sequelize.models as MyOwnDefinedTypes) ?

chanlito commented 6 years ago
screen shot 2017-11-20 at 8 47 37 pm
export interface IModels {
  Todo: Model<Todo, any>; // Why can't I just use typeof Todo here?
  [x: string]: Model<any, any>;
}

// after sequelize initialize somewhere...
const models = sequelize.models as IModels;

Seems strange about that typings.

@RobinBuschmann I think this should be compatible with the way we define the models in sequelize-typescript.

RobinBuschmann commented 6 years ago

You mean https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/sequelize/index.d.ts#L5755 , right?

Unfortunately this is not compatible with the way sequelize-typescript defines its Model. So that sequelize.Model !== sequelize-typescript.Model. For sequelize-typescriptit needs to look like:

    interface ModelsHashInterface {
        [name: string]: typeof Model;
    }
RobinBuschmann commented 6 years ago

Ahh, and some 🎉🎉🎉🎉🎉 for the 200th issue 😀

chanlito commented 6 years ago

Wow 200 issues already, didn't noticed it til u mentioned. 😄 🤣

chanlito commented 6 years ago

@RobinBuschmann how would I override the existing type definition? I'm not experienced in this area. Been googling several hours now.

RobinBuschmann commented 6 years ago

Hmm, I see, overriding will not work. You will always get an TS2415:Class 'XXX' incorrectly extends base class 'YYY'. Types of property 'aaa' are incompatible. Type 'ccc' is not assignable to type 'bbb'. , right?

I'm afraid that we need to do the same with Sequelize.d.ts what we did with Model.d.ts to achieve this. So rewriting the whole typings for the Sequelize class.

chanlito commented 6 years ago

😩 Guess I'll have to wait for next beta release.

RobinBuschmann commented 6 years ago

Fixed by 87cc694

RobinBuschmann commented 5 years ago

Released with 1.0.0-alpha.X