lmcq / firebase-firestorm

Firebase Firestorm is an ORM for Firestore which can be used with Typescript.
MIT License
252 stars 19 forks source link

Constructor in subCollection Error: Type 'typeof foo' is not assignable to type 'new () => IBarInterface' #28

Closed jones1008 closed 3 years ago

jones1008 commented 3 years ago

I have a TypeScript class Game with a subCollection class Team.

// Game.ts
import {Entity, rootCollection, subCollection, ICollection} from 'firebase-firestorm';

@rootCollection({
    name: 'games'
})
export default class Game extends Entity {
    @subCollection({
        name: 'teams',
        entity: Team  // the error refers to this line
    })
    teams: ICollection<Team>;
}
// Team.ts
import {Entity, field} from "firebase-firestorm";

export default class Team extends Entity {
    @field({name: 'name'})
    name: string

    constructor(name: string) {
        super();
        this.name = name;
    }
}

As soon as I add a constructor to the Team class I get the following TypeScript error:

Error:(22, 9) TS2322: Type 'typeof Team' is not assignable to type 'new () => IEntity'.

I don't have this error if I don't have a constructor within the Team class.

Is it not possible to use a constructor within a subCollection class?

jones1008 commented 3 years ago

Turns out you have to add a default value to the parameter of the constructor as described here to get rid of this error.

So I changed my class Team into this:

export default class Team extends Entity {
    @field({name: 'name'})
    name: string

    constructor(name: string = '') {
        super();
        this.name = name;
    }
}

I don't know why this is needed but it works...

Seems to be an issue with TypeScript and not firebase-firestorm.