Skip to content

使用 TypeScript 开发时,如何与 mobx-miniprogram-bindings 共存 #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wxin2022 opened this issue Jul 24, 2022 · 5 comments
Open

Comments

@wxin2022
Copy link

使用 ts 进行开发时,computed 推荐使用 ComponentWithComputed 替代 微信的 Component 方法构造页面,然而 mobx-miniprogram 推荐 使用 ComponentWithStore 进行构造页面,那么请问如何才能使两者共存?

@TtTRz
Copy link
Member

TtTRz commented Jul 27, 2022

对这块的 TS 支持确实有所欠缺,后续会完善下。

@ginlink
Copy link

ginlink commented Jan 31, 2023

目前没有找到好方法,我选择放弃store的类型声明,因为一般情况下,computed用得会更多

@copofe
Copy link

copofe commented Feb 28, 2023

目前没有找到好方法,我选择放弃store的类型声明,因为一般情况下,computed用得会更多

可以组合两个的类型声明,重写个

import { behavior } from 'miniprogram-computed';
import { storeBindingsBehavior, IStoreBindings } from 'mobx-miniprogram-bindings';

type ComponentWithInstance<
  D extends WechatMiniprogram.Component.DataOption,
  P extends WechatMiniprogram.Component.PropertyOption,
  M extends WechatMiniprogram.Component.MethodOption,
  C extends Record<string, (data: D & { [K in keyof P]: any }) => any>,
  CP extends WechatMiniprogram.IAnyObject = Record<string, never>
> = WechatMiniprogram.Component.Instance<D, P, M, CP> & {
  data: { [K in keyof C]: ReturnType<C[K]> } & { [K in keyof P]: any };
};

type WithOptions<
  TData extends WechatMiniprogram.Component.DataOption,
  TProperty extends WechatMiniprogram.Component.PropertyOption,
  TMethod extends WechatMiniprogram.Component.MethodOption,
  TWatch extends Record<string, (...args: any[]) => void>,
  TComputed extends Record<
    string,
    (data: TData & WechatMiniprogram.Component.PropertyOptionToData<TProperty>) => any
  >,
  TCustomInstanceProperty extends WechatMiniprogram.IAnyObject = {},
> = (Partial<WechatMiniprogram.Component.Data<TData>> &
  Partial<WechatMiniprogram.Component.Property<TProperty>> &
  Partial<WechatMiniprogram.Component.Method<TMethod>> &
  Partial<WechatMiniprogram.Component.OtherOption> &
  Partial<WechatMiniprogram.Component.Lifetimes> & {
    watch?: TWatch;
    computed?: TComputed;
    template?: string;
    storeBindings?: IStoreBindings | Array<IStoreBindings>;
  }) &
  ThisType<
    ComponentWithInstance<
      TData,
      TProperty,
      TMethod,
      TComputed,
      TCustomInstanceProperty
    >
  >;

export function ComponentWith<
  TData extends WechatMiniprogram.Component.DataOption,
  TProperty extends WechatMiniprogram.Component.PropertyOption,
  TMethod extends WechatMiniprogram.Component.MethodOption,
  TWatch extends Record<string, (...args: any[]) => void>,
  TComputed extends Record<
    string,
    (data: TData & WechatMiniprogram.Component.PropertyOptionToData<TProperty>) => any
  >,
  TCustomInstanceProperty extends WechatMiniprogram.IAnyObject = {},
>(options: WithOptions<
  TData,
  TProperty,
  TMethod,
  TWatch,
  TComputed,
  TCustomInstanceProperty>) {
  if (!Array.isArray(options.behaviors)) {
    options.behaviors = [];
  }
  options.behaviors.unshift(storeBindingsBehavior, behavior);
  return Component(options);
}

@Tommykkkk
Copy link

你好,使用 ts 进行开发时,computed 推荐使用 ComponentWithComputed 替代 微信的 Component 方法构造页面 ,ts会发生报错问题 比如使用本身this.properties.A 的传递进来的参数ts提示 类型“{ onSelect(event: CustomEvent): void; onAdd(event: CustomEvent): void; beforExportData(arr: Comp[], CompCode: string): void; }”上不存在属性“properties”。这块内容应该如何去调整

@LastLeaf
Copy link
Member

LastLeaf commented Jan 9, 2025

你好,使用 ts 进行开发时,computed 推荐使用 ComponentWithComputed 替代 微信的 Component 方法构造页面 ,ts会发生报错问题 比如使用本身this.properties.A 的传递进来的参数ts提示 类型“{ onSelect(event: CustomEvent): void; onAdd(event: CustomEvent): void; beforExportData(arr: Comp[], CompCode: string): void; }”上不存在属性“properties”。这块内容应该如何去调整

请确认:

  • tsconfig.json 中的 noImplicitThis 选项已经被打开;
  • 实际使用的 miniprogram-api-typings 版本 >= 4.0.0 。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants