api-typings icon indicating copy to clipboard operation
api-typings copied to clipboard

page泛型写了DataOption不应该强制传CustomOption

Open AntzyMo opened this issue 3 years ago • 10 comments

我的用途是想限制data的类型,方法那些不强制传CustomOption泛型 可以给CustomOption设置默认值

AntzyMo avatar Aug 09 '22 08:08 AntzyMo

I have the same problem. I have to define the function type in WechatMiniprogram.Page.Options's TCustom parameter which is unnecessary. image

Is there any way to only define the typings of data fields and the non-function field of TCustom when using WechatMiniprogram.Page.Options?

BilderLoong avatar Sep 29 '22 03:09 BilderLoong

你可以在使用泛型时继续声明一个新的接口保留你没用到的泛型。这是TS不熟的问题。

Mister-Hope avatar Sep 29 '22 06:09 Mister-Hope

你可以在使用泛型时继续声明一个新的接口保留你没用到的泛型。这是TS不熟的问题。

Sorry, I can't understand what you mean. I would very much appreciate that if you could provide an example.

BilderLoong avatar Sep 29 '22 07:09 BilderLoong

你们可以自行参考 https://github.com/miniapp-tool/mptool/tree/main/packages/enhance 。 这是一个我写的小框架用于组件打通和劫持router以提供预加载逻辑。里面有对页面、App和组件构造器的改动与二次扩展。而且保留了与原来一致的行为接口。

Mister-Hope avatar Sep 29 '22 07:09 Mister-Hope

为什么不写个默认值呢

AntzyMo avatar Sep 29 '22 07:09 AntzyMo

默认值在一些情况下可能会影响泛型推导。这边建议你俩把ts好好学学。

Mister-Hope avatar Sep 29 '22 07:09 Mister-Hope

或者再直白一点,没人阻止你们构造一个类似这样的东西:https://github.com/miniapp-tool/mptool/blob/6d002128c9b05b33a86f4f8c6d96803f3b45ceda/packages/enhance/src/page/typings.ts#L131-L142

你直接构造一个,然后把你想要写死的泛型去掉就好了

Mister-Hope avatar Sep 29 '22 07:09 Mister-Hope

或者再直白一点,没人阻止你们构造一个类似这样的东西:https://github.com/miniapp-tool/mptool/blob/6d002128c9b05b33a86f4f8c6d96803f3b45ceda/packages/enhance/src/page/typings.ts#L131-L142

你直接构造一个,然后把你想要写死的泛型去掉就好了

Did you mean to create a new type that provides a default type for the TCustom type parameter, like the below:

    type Options<
        TData extends DataOption,
        TCustom extends CustomOption = any
    > = (TCustom & Partial<Data<TData>> & Partial<ILifetime>) &
        ThisType<Instance<TData, TCustom>>

BilderLoong avatar Sep 29 '22 11:09 BilderLoong