Skip to content

API 泛型添加 #177

Description

@Mister-Hope

希望能够为所有返回 any 的api添加泛型,如:

        getStorageSync(
            /** 本地缓存中指定的 key */
            key: string
        ): any

改为

        getStorageSync<T = any>(
            /** 本地缓存中指定的 key */
            key: string
        ): T

再如:

    interface RequestOption {
        ...
        /** 请求的参数 */
        data?: string | IAnyObject | ArrayBuffer
        ...
        /** 设置请求的 header,header 中不能设置 Referer。
         *
         * `content-type` 默认为 `application/json` */
        header?: IAnyObject
        ...
    }

    request(option: RequestOption): RequestTask

改为

    interface RequestOption<DataOption = string | IAnyObject | ArrayBuffer, HeaderOption extends IAnyObject> {
        ...
        /** 请求的参数 */
        data?: DataOption
        ...
        /** 设置请求的 header,header 中不能设置 Referer。
         *
         * `content-type` 默认为 `application/json` */
        header?: HeaderOption 
        ...
    }

    request<DataOption = string | IAnyObject | ArrayBuffer, HeaderOption extends IAnyObject>(option: RequestOption<DataOption, HeaderOption>): RequestTask

我个人是非常抵制 any 在 typescript 的使用的,遍地 any 约等于没用 typescript。
image

而满地 as 显然不够优雅:

// 判断是否已经显示过
const cache = wx.getStorageSync("add-tip") as numebr;

所以希望最好能够支持

```ts
// 判断是否已经显示过
const cache = wx.getStorageSync<number>("add-tip");

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions