wechat-miniprogram / wechat-miniprogram/api-typings

API 泛型添加

Open
#177 4 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
803
Forks
117
Avg merge
24m
Merged PRs (30d)
1

Description

希望能够为所有返回 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");

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by surveying the TypeScript API declarations for any, using getStorageSync, RequestOption, and request as the examples given in the issue. Determine the intended generic scope and compatibility expectations before changing declarations; done should mean the targeted APIs support useful inferred or explicit types without the existing any usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.