为了帮助this person,我正在删除Ampersand.js库的打字稿定义。鉴于以下打字稿代码,为什么编译器会输出无效的'新'预期错误?
[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' });
“新”表达无效。
答案 0 :(得分:0)
我的extend方法是返回一个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' });