【问题标题】:Typescript: Invalid 'new' expression打字稿:无效的“新”表达式
【发布时间】:2014-08-11 22:17:17
【问题描述】:

为了帮助this person,我正在删除Ampersand.js 库的打字稿定义。给定以下打字稿代码,为什么编译器会输出 Invalid 'new' experession 错误?

[Here's a link to run the code using the typescript playground.]

declare module ampersand {
    interface AmpersandState {
        // todo...
    }

    interface AmpersandCollection {
        // todo...
    }

    interface ModelExtendOptions {
        parse?: boolean;
        parent?: AmpersandState;
        collection?: AmpersandCollection;
    }

    interface ModelSaveOptions {
        patch?: boolean;
    }

    class AmpersandModel<T> {
        constructor(attrs: T, options?: ModelExtendOptions);
        save: (attrs?: T, options?: ModelSaveOptions) => void;
        // todo:  fetch, destroy, sync, etc...
    }

    interface ExtendOptions {
        props?: {};
        session?: {};
        derived?: {};
    }   

    interface AmpersandModelStatic {
        extend: <T>(options: ExtendOptions) => AmpersandModel<T>;
    }
} 

declare var AmpersandModel: ampersand.AmpersandModelStatic;


interface PersonProps {
    firstName: string;
    lastName: string;
}

var Person = AmpersandModel.extend<PersonProps>({ props: { firstName: 'string', lastName: 'string' } });

var me = new Person({ firstName: 'Phil', lastName: 'Roberts' });

“新”表达式无效。

【问题讨论】:

    标签: typescript ampersand


    【解决方案1】:

    我的扩展方法返回的是 AmpersandModel 的一个实例,而不是 ApersandModel 的构造函数。更正代码:

    declare module ampersand {
        interface AmpersandState {
            // todo...
        }
    
        interface AmpersandCollection {
            // todo...
        }
    
        interface ModelExtendOptions {
            parse?: boolean;
            parent?: AmpersandState;
            collection?: AmpersandCollection;
        }
    
        interface ModelSaveOptions {
            patch?: boolean;
        }
    
        interface AmpersandModel<T> {
            save: (attrs?: T, options?: ModelSaveOptions) => void;
            // todo:  fetch, destroy, sync, etc...
        }
    
        interface AmpersandModelConstructor<T> {
            new (attrs: T, options?: ModelExtendOptions): AmpersandModel<T>;
        }   
    
        interface ExtendOptions {
            props?: {};
            session?: {};
            derived?: {};
        }   
    
        interface AmpersandModelStatic {
            extend: <T>(options: ExtendOptions) => AmpersandModelConstructor<T>;
        }
    } 
    
    declare var AmpersandModel: ampersand.AmpersandModelStatic;
    
    interface PersonProps {
        firstName: string;
        lastName: string;
    }
    
    interface IPerson extends PersonProps, ampersand.AmpersandModel<PersonProps>{}
    
    var Person = AmpersandModel.extend<PersonProps>({ props: { firstName: 'string', lastName: 'string' } });
    
    var me = new Person({ firstName: 'Jeremy', lastName: 'Danyow' });
    

    【讨论】:

      猜你喜欢
      • 2017-10-10
      • 2012-11-24
      • 2013-01-16
      • 2023-02-09
      • 2019-04-25
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      • 2019-06-22
      相关资源
      最近更新 更多