mobx-miniprogram-bindings
mobx-miniprogram-bindings copied to clipboard
ComponentWithStore 的 TS 支持
在使用 ComponentWithStore 时发现,所有的 this.data, this.properties 的类型都变成了 Function。
看了下你们写的类型声明,发现是有问题的
declare type TData = WechatMiniprogram.Component.DataOption;
declare type TProperty = WechatMiniprogram.Component.PropertyOption;
declare type TMethod = WechatMiniprogram.Component.MethodOption;
declare type StoreOptions = Partial<WechatMiniprogram.Component.Data<TData>> & Partial<WechatMiniprogram.Component.Property<TProperty>> & Partial<WechatMiniprogram.Component.Method<TMethod>> & Partial<WechatMiniprogram.Component.OtherOption> & Partial<WechatMiniprogram.Component.Lifetimes> & {
storeBindings?: IStoreBindings | Array<IStoreBindings>;
};
export declare function ComponentWithStore(options: StoreOptions): string;
// ...
export function ComponentWithStore(options: StoreOptions): string {
// TODO invoke behavior
if (!Array.isArray(options.behaviors)) {
options.behaviors = [];
}
options.behaviors.unshift(behavior);
return Component(options);
}
这里的 ComponentWithStore 应该接收泛型传递给函数内部的 Component 构造器,否则会丢失类型。
可以参考小程序 computed 的类型声明方式 https://github.com/wechat-miniprogram/computed/blob/master/types/index.d.ts https://github.com/wechat-miniprogram/computed/blob/master/src/index.ts
理想的情况是像 Component 那样通过泛型声明😂
ComponentWithStore<IStoreInstance, IData, IProperty, IMethod, IOther...>({ ... })
可以参考小程序 computed 的类型声明方式 https://github.com/wechat-miniprogram/computed/blob/master/types/index.d.ts https://github.com/wechat-miniprogram/computed/blob/master/src/index.ts
理想的情况是像 Component 那样通过泛型声明😂
ComponentWithStore<IStoreInstance, IData, IProperty, IMethod, IOther...>({ ... })
确实有点问题,下一个小版本修复