使用Typescript 2.4.1不能赋予类型错误

时间:2017-07-12 17:34:57

标签: javascript node.js typescript generics

我正在使用打字稿开发NodeJS应用程序。我已经将打样稿更新到2.4.1了,而且我有一个"不能分配到" Generics类错误。我已经读过新版本改进了对泛型的检查(https://blogs.msdn.microsoft.com/typescript/2017/06/27/announcing-typescript-2-4/)。但我仍然无法看到我的代码有什么问题......

我有一家工厂:

    export abstract class EntityClass {
        public abstract getTable (): string
    }

    export class FactoryClass {
        public getRepository<T extends EntityClass> (target): Repository<T> {
            return new Repository<T>(target)
        }
    }

我的存储库类:

    export interface NxtDbColumnValuePair {
        column: string
        value: any
    }

    export class Db {
        public insert (): Db {
            return this
        }

        public setTable (table: string) {
            return this
        }

        public setColumnsValuesPair (colValPairs: NxtDbColumnValuePair[]) {
            return this
        }

        public execute (): Promise<any> {
            return Promise.resolve({ inserId: 1 })
        }
    }

    export class Repository<T extends EntityClass> {

        private db: Db = new Db()

        constructor (private entity) {}

        public insert (entityInstance: T): Promise<T> {
            const propertiesNameEntity = Object.getOwnPropertyNames(entityInstance)

            const colValPairs: NxtDbColumnValuePair[] = propertiesNameEntity
                .map((prop: string) => {
                    return {
                        column: prop,
                        value: entityInstance[prop],
                    }
                })

            return this.db.insert() // Insert value in SQL table
                .setTable(entityInstance.getTable())
                .setColumnsValuesPair(colValPairs)
                .execute()
                .then((results) => results.inserId) // Get new id inserted
                .then((id) => this.find(id)) // get entity by id and return it
        }

        public find (id?: number): Promise<T> { // I didn't put the code for this method but this is the definition
            return Promise.resolve(new this.entity())
        }
    }

这就是代码无法运作的地方:

    export class User {}

    export class Client {}

    export class Token extends EntityClass {
        public user: User
        public client: Client
        public token: string

        public getTable (): string {
            return 'token'
        }
    }

    export class MyClass {
        public generateNewToken (user: User, client: Client): Promise<Token> {
            const factory: FactoryClass = new FactoryClass()
            const token: string = '123456789' // Generated randomly

            const tokenClass: Token = new Token() // this class extends EntityClass
            tokenClass.user = user
            tokenClass.client = client
            tokenClass.token = token

            return factory.getRepository(Token).insert(tokenClass) // Error here !
        }
    }

错误是:输入&#39;承诺&lt; EntityClass&gt;&#39;不能分配类型&#39; Promise&lt;令牌&gt;&#39;。它适用于typescript 2.2.1。

谢谢

0 个答案:

没有答案