├── .eslintrc.js
├── .github
└── workflows
│ └── test.yml
├── .gitignore
├── .npmrc
├── CHANGELOG.md
├── LICENSE
├── README-en.md
├── README.md
├── index.d.ts
├── package.json
├── pnpm-lock.yaml
├── test
├── .eslintrc.js
├── api-doc.test.ts
├── api-extend.test.d.ts
├── api-extend.test.ts
├── api-promisify.test.ts
├── api.test.ts
├── app.test.ts
├── behavior.test.ts
├── component.test.ts
├── computed.test.d.ts
├── computed.test.ts
├── index.d.ts
├── issue.test.ts
├── page.test.ts
└── xr-frame.test.ts
├── tsconfig.json
├── types
├── index.d.ts
└── wx
│ ├── index.d.ts
│ ├── lib.wx.api.d.ts
│ ├── lib.wx.app.d.ts
│ ├── lib.wx.behavior.d.ts
│ ├── lib.wx.canvas.d.ts
│ ├── lib.wx.cloud.d.ts
│ ├── lib.wx.component.d.ts
│ ├── lib.wx.event.d.ts
│ ├── lib.wx.page.d.ts
│ ├── lib.wx.phys3D.d.ts
│ ├── lib.wx.wasm.d.ts
│ └── lib.wx.xr-frame.d.ts
└── typings.json
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | root: true,
3 | parser: '@typescript-eslint/parser',
4 | parserOptions: {
5 | project: './tsconfig.json'
6 | },
7 | plugins: [
8 | '@typescript-eslint',
9 | ],
10 | rules: {
11 | indent: ['error', 4],
12 | 'no-trailing-spaces': ['error'],
13 | semi: ['off'],
14 | '@typescript-eslint/semi': ['error', 'never'],
15 | '@typescript-eslint/no-extra-semi': ['error'],
16 | quotes: ['off'],
17 | '@typescript-eslint/quotes': ['error', 'single'],
18 | '@typescript-eslint/ban-ts-comment': ['off'],
19 | '@typescript-eslint/adjacent-overload-signatures': ['error'],
20 | '@typescript-eslint/ban-types': ['error', {
21 | extendDefaults: true,
22 | types: {
23 | /**
24 | * we are using `{}` as noop
25 | * e.g. `type A
= B & (P extends Q ? C : {})`
26 | * will get `B & C` when `P extends Q` and `B` otherwise
27 | */
28 | '{}': false,
29 | /**
30 | * we actually need a type accepting any function-like value
31 | * e.g. `type Methods = Record`
32 | */
33 | 'Function': false,
34 | }
35 | }],
36 | '@typescript-eslint/member-delimiter-style': ['error', {
37 | multiline: {
38 | delimiter: 'none',
39 | requireLast: false,
40 | },
41 | singleline: {
42 | delimiter: 'comma',
43 | requireLast: false
44 | }
45 | }],
46 | '@typescript-eslint/naming-convention': ['error', {
47 | selector: 'enum',
48 | format: ['PascalCase', 'UPPER_CASE'],
49 | leadingUnderscore: 'forbid',
50 | trailingUnderscore: 'forbid',
51 | }, {
52 | selector: 'typeLike',
53 | format: ['PascalCase'],
54 | leadingUnderscore: 'forbid',
55 | trailingUnderscore: 'forbid',
56 | }],
57 | '@typescript-eslint/array-type': ['error', {
58 | default: 'array-simple',
59 | readonly: 'array-simple'
60 | }],
61 | '@typescript-eslint/no-unnecessary-qualifier': ['error'],
62 | },
63 | }
--------------------------------------------------------------------------------
/.github/workflows/test.yml:
--------------------------------------------------------------------------------
1 | name: Test
2 |
3 | on:
4 | - push
5 | - pull_request
6 |
7 | jobs:
8 | test:
9 | runs-on: ubuntu-latest
10 | steps:
11 | - name: Checkout
12 | uses: actions/checkout@v4
13 |
14 | - uses: pnpm/action-setup@v4
15 | name: Install pnpm
16 | with:
17 | version: latest
18 | run_install: false
19 |
20 | - name: Install Node.js
21 | uses: actions/setup-node@v4
22 | with:
23 | node-version: lts/*
24 | cache: 'pnpm'
25 |
26 | - name: Install dependencies
27 | run: pnpm install --frozen-lockfile
28 |
29 | - name: Run test
30 | run: npm run test
31 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # npm
2 | node_modules
3 |
4 | # OS X
5 | .DS_Store*
6 | Icon?
7 | ._*
8 |
--------------------------------------------------------------------------------
/.npmrc:
--------------------------------------------------------------------------------
1 | registry=https://registry.npmjs.org
2 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ## 2025-04-18 v4.0.7
2 | - 更新云开发拓展能力类型定义
3 |
4 | ## 2025-04-18 v4.0.6
5 | - 更新 API 定义到 3.8.0
6 |
7 | ## 2025-02-08 v4.0.5
8 | - 修复 `setTimeout` 和 `setInterval` 的参数 ([#323](https://github.com/wechat-miniprogram/api-typings/issues/323))
9 | - 更新 API 定义到 3.7.7
10 |
11 | ## 2025-01-14 v4.0.4
12 | - 解决依赖安全问题
13 |
14 | ## 2025-01-14 v4.0.3
15 | - 补齐云开发 `CollectionReference` 上的 `aggregate` ([#276](https://github.com/wechat-miniprogram/api-typings/issues/276))
16 | - 补齐组件实例方法 `setInitialRenderingCache` 和 `getAppBar` by [@Yang Mingshan](https://github.com/yangmingshan) ([#339](https://github.com/wechat-miniprogram/api-typings/issues/339))
17 | - 更新 API 定义到 3.7.4
18 |
19 | ## 2024-11-13 v4.0.2
20 | - 补齐 `RaycastHit` ([#337](https://github.com/wechat-miniprogram/api-typings/issues/337))
21 |
22 | ## 2024-09-24 v4.0.1
23 | - 更新 API 定义到 3.5.7
24 |
25 | ## 2024-09-24 v4.0.0
26 | 合入 [#332](https://github.com/wechat-miniprogram/api-typings/pull/332), [#333](https://github.com/wechat-miniprogram/api-typings/pull/333), [#334](https://github.com/wechat-miniprogram/api-typings/pull/334) by [@lvzl](https://github.com/lv-z-l)。这几个 Pull Request 对 `Component` 和 `Behavior` 的实现进行了较大改动,以支持:
27 | 1. 对于 `Array` 和 `Object` 类型的 `property` 和 `data`,以值的实际类型作为泛型推导的结果,而非固定推导为 `any[]` 和 `Record`;
28 | 2. 改变了 `BehaviorIdentifier` 的类型,通过为其交叉一个带有 `Behavior` 定义信息的虚假类型,使 `Component` 和 `Behavior` 能自动推导其使用到的 `Behavior` 的 `data`, `properties` 及 `methods`;
29 |
30 | 这是一个比较大的 **破坏性改动**,从低于 4.0.0 的版本升级时,可能需要进行一些适配:
31 | 1. 全局函数 `Component` 和 `Behavior` 的第四个泛型现在是新的 `TBehavior`,如果之前的代码中有为这两个函数手动指定泛型的用例,需要手动添加这个泛型;
32 | 2. `Behavior()` 的返回值不再是 `string`(或者说不完全是),之前使用 `string` 接受 `Behavior()` 返回值的实现需要修改为 `WechatMiniprogram.Behavior.BehaviorIdentifier`;
33 | 3. 自定义组件的 `this.data` 和 `this.property` 的推导类型可能会有所变化,需要根据推导类型进行相应改动。
34 |
35 | ## 2024-08-08 v3.12.3
36 | - 更新 API 定义到 3.5.2
37 | - 修复 [#235](https://github.com/wechat-miniprogram/api-typings/issues/235), [#302](https://github.com/wechat-miniprogram/api-typings/issues/302), [#303](https://github.com/wechat-miniprogram/api-typings/issues/303), [#304](https://github.com/wechat-miniprogram/api-typings/issues/304) by [@Yang Mingshan](https://github.com/yangmingshan)
38 |
39 | ## 2023-12-01 v3.12.2
40 | - 更新 API 定义到 3.2.3
41 |
42 | ## 2023-10-17 v3.12.1
43 | - 更新 API 定义到 3.1.2
44 | - 补齐自定义组件实例的 `getPassiveEvent`, `setPassiveEvent` 方法
45 |
46 | ## 2023-08-24 v3.12.0
47 | - `App` 生命周期 `onLaunch`, `onShow` 参数中的 `referrerInfo` 字段类型对齐 API 定义中的 `ReferrerInfo`。这是一个 **破坏性改动**,其中 `extraData` 的类型从 `any` 收窄到了 `Record`
48 | - 根据实际实现,修改了 `LaunchOptions` 中 `query` 字段的类型。这是一个 **破坏性改动**,该类型从 `Record` 收窄到了 `Record`
49 |
50 | ## 2023-08-24 v3.11.1
51 | - 更新 API 定义到 3.0.1
52 |
53 | ## 2023-08-04 v3.11.0
54 | - 更新 API 定义到 3.0.0
55 | - 暂未支持 [glass-easel](https://github.com/wechat-miniprogram/glass-easel) [Chaining API](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/glass-easel/chaining-api.html) 的类型定义
56 | - 补齐 `WXWebAssembly` 定义
57 |
58 | ## 2023-06-09 v3.10.0
59 | - 更新 API 定义到 2.32.1
60 | - 新增 CanvasRenderingContext 类型定义 [#111](https://github.com/wechat-miniprogram/api-typings/issues/111)
61 |
62 | ## 2023-04-10 v3.9.1
63 | - 更新 API 定义到 2.30.4
64 | - 修复页面 `onShareAppMessage` 异步形式的定义错误
65 |
66 | ## 2023-01-12 v3.9.0
67 | - 更新 API 定义到 2.29.1
68 | - 将 xr-frame 的命名空间由 `WechatXrFrame` 改为 `XrFrame`。这是一个 **破坏性改动**
69 |
70 | ## 2022-09-09 v3.6.0
71 | - 更新 API 定义到 2.26.0
72 | - 更改了部分监听方法及其参数的命名
73 |
74 | ## 2022-06-24 v3.5.0
75 | - 更新 API 定义到 2.24.6
76 |
77 | ## 2022-04-01 v3.4.6
78 | - 更新 API 定义到 2.23.2
79 |
80 | ## 2022-01-20 v3.4.5
81 | - 更新 API 定义到 2.21.3
82 |
83 | ## 2021-08-24 v3.4.4
84 | - 更新 API 定义到 2.20.1
85 |
86 | ## 2021-08-24 v3.4.3
87 | - 更新 API 定义到 2.19.2
88 | - 补充自定义组件获取更新性能接口定义
89 |
90 | ## 2021-08-02 v3.4.2
91 | - 更新 API 定义到 2.19.0
92 | - 重新整理了注释,包括:
93 | - 加入插件支持情况、版本和说明
94 | - 将支持和废弃情况挪到前面,使其更不容易因为接口说明太长而被忽略
95 | - 移除文首、文末和多余(连续超过两个)的空行
96 | - 修复几个链接
97 |
98 | ## 2021-07-07 v3.4.1
99 | - 移除一个意外加入的非预期字符
100 |
101 | ## 2021-07-07 v3.4.0
102 | - 更新 API 定义到 2.18.0
103 | - 更新来自文档代码示例的测试用例
104 | - 更新 npm 依赖以解决安全问题
105 | - 修复 [#202](https://github.com/wechat-miniprogram/api-typings/issues/202), [#204](https://github.com/wechat-miniprogram/api-typings/issues/204)
106 |
107 | ## 2021-04-21 v3.3.2
108 | - 更新 API 定义到 2.16.1
109 |
110 | ## 2021-04-09 v3.3.1
111 | - 更新 API 定义到 2.16.0
112 |
113 | ## 2021-03-02 v3.3.0
114 | - 更新部分新接口定义
115 | - 支持泛型([#177](https://github.com/wechat-miniprogram/api-typings/issues/177))
116 | - 支持索引签名,以支持 `wx.requestSubscribeMessage`([#175](https://github.com/wechat-miniprogram/api-typings/issues/175))
117 |
118 | ## 2021-02-22 v3.2.3
119 | - 更新 API 定义到 2.15.0
120 | - 修复 `Component.triggerEvent` 的 `detail` 类型
121 | - 修复几个接口的定义([#193](https://github.com/wechat-miniprogram/api-typings/issues/193), [#185](https://github.com/wechat-miniprogram/api-typings/issues/185), [#180](https://github.com/wechat-miniprogram/api-typings/issues/180))
122 | - 修改 `MethodOption` 以解决 [#161](https://github.com/wechat-miniprogram/api-typings/issues/161)(鸣谢:[@Lienviws](https://github.com/Lienviws))
123 |
124 | ## 2021-01-14 v3.2.2
125 | - 修复几个接口未 Promise 化的问题
126 |
127 | ## 2021-01-06 v3.2.1
128 | - 更新 API 定义到 2.14.1
129 | - 补齐 `virtualHost` ([#174](https://github.com/wechat-miniprogram/api-typings/issues/174))
130 |
131 | ## 2020-11-13 v3.2.0
132 | - 更新 API 定义到 2.14.0
133 | - 补齐 NFC 接口的错误码
134 |
135 | ## 2020-11-04 v3.1.6
136 | - 补齐 `requirePlugin` 和 `requireMiniProgram`
137 |
138 | ## 2020-10-29 v3.1.5
139 | - 更新 API 定义
140 | - 修复代码格式问题(`no-unnecessary-qualifier`)
141 |
142 | ## 2020-10-28 v3.1.4
143 | - 更新 API 定义到 2.13.2
144 | - 为被废弃的接口增加了 `@deprecated` 标识
145 |
146 | ## 2020-10-14 v3.1.3
147 | - 修复 `ICustomTimelineContent` 的 `query` 的类型
148 |
149 | ## 2020-09-30 v3.1.2
150 | - 更新 API 定义到 2.13.1
151 |
152 | ## 2020-09-24 v3.1.1
153 | - 更新 API 定义到 2.13.0
154 | - 改变了嵌套命名空间的写法
155 | - 支持 `Component` 的第五个泛型参数,用于将自定义组件作为页面根组件使用的情况
156 |
157 | ## 2020-09-22 v3.1.0
158 | - 将代码风格检查从 tslint 迁移到 eslint
159 |
160 | ## 2020-08-19 v3.0.2
161 | - 更新 API 定义
162 | - 合并 PR [#151](https://github.com/wechat-miniprogram/api-typings/pull/151), [#152](https://github.com/wechat-miniprogram/api-typings/pull/152),补齐事件类型,补齐 `onShareTimeline`
163 |
164 | ## 2020-08-19 v3.0.2
165 | - 更新 API 定义
166 | - 合并 PR [#124](https://github.com/wechat-miniprogram/api-typings/pull/124), [#145](https://github.com/wechat-miniprogram/api-typings/pull/145),修复两个动画接口的问题
167 |
168 | ## 2020-08-03 v3.0.1
169 | - 更新 API 定义
170 | - 修复某些取消监听的接口(`off`)的参数不为可选值的问题
171 |
172 | ## 2020-07-30 v3.0.0
173 | - 由于基础库接口基本向前兼容,不再与基础库保持版本同步
174 | - 更新 API 定义到 2.12.0
175 |
176 | ## 2020-06-15 v2.11.0-1
177 | - 该版本继续合并了一部分完全相同的 interface / callback,是一个 **破坏性改动**,原本字面上引用了这些 interface / callback 的代码可能会报错。
178 | - 为 `Component` 构造器增加第四个泛型,以允许在自定义组件上挂载自定义的字段 ([#133](https://github.com/wechat-miniprogram/api-typings/issues/133))
179 | - 修复一些接口错误 ([#134](https://github.com/wechat-miniprogram/api-typings/issues/134))
180 | - 补齐 `App` 的 `onThemeChange` ([#135](https://github.com/wechat-miniprogram/api-typings/issues/135))
181 | - 补齐 `Page` 的 `onAddToFavorites` ([#136](https://github.com/wechat-miniprogram/api-typings/issues/136))
182 |
183 | ## 2020-05-20 v2.11.0
184 | - 同步 API 定义到基础库 2.11.0
185 | - 该版本继续合并了一部分完全相同的 interface / callback,是一个 **破坏性改动**,原本字面上引用了这些 interface / callback 的代码可能会报错。
186 | - 修复接口错误 ([#126](https://github.com/wechat-miniprogram/api-typings/issues/126))
187 |
188 | ## 2020-04-20 v2.10.4
189 | - 同步 API 定义到基础库 2.10.4
190 | - 在之前的版本中,分属于不同接口的两个 interface / callback 即使完全相同,也会拥有不同的名字。在这次更新中,他们将合并为同一个(如 `FileSystemManagerGetFileInfoCompleteCallback` 和 `WxGetFileInfoCompleteCallback` 都变成了 `GetFileInfoCompleteCallback`)。这是一个 **破坏性改动**,原本字面上引用了这些 interface / callback 的代码可能会报错。
191 | - 修复了一些取消监听接口(off callback)的参数错误 ([#120](https://github.com/wechat-miniprogram/api-typings/issues/120))
192 |
193 | ## 2020-04-03 v2.10.3-1
194 | - 补齐 `Component` 的 `getOpenerEventChannel` ([#112](https://github.com/wechat-miniprogram/api-typings/issues/113) by [@baranwang](https://github.com/baranwang))
195 | - 加入了部分事件的定义 ([#115](https://github.com/wechat-miniprogram/api-typings/issues/115) by [@zenml](https://github.com/zenml))
196 | - 更新了小程序·云开发的 API 定义 ([#92](https://github.com/wechat-miniprogram/api-typings/issues/92))
197 |
198 | ## 2020-03-26 v2.10.3
199 | - 同步 API 定义到基础库 2.10.3
200 |
201 | ## 2020-03-18 v2.10.2-1
202 | - 支持 API Promise 化调用 ([#105](https://github.com/wechat-miniprogram/api-typings/issues/105))
203 |
204 | ## 2020-03-06 v2.10.2
205 | - 同步 API 定义到基础库 2.10.2
206 |
207 | ## 2020-02-10 v2.10.1-1
208 | - 允许重写部分全局变量 (由 `const` 改为 `let`) ([#102](https://github.com/wechat-miniprogram/api-typings/issues/102))
209 | - 补齐 `Page` 上的 `options` 字段 ([#101](https://github.com/wechat-miniprogram/api-typings/issues/101) by [@baranwang](https://github.com/baranwang))
210 |
211 | ## 2020-01-19 v2.10.1
212 | - 同步 API 定义到基础库 2.10.1
213 | - 补齐 `Component` `selectOwnerComponent`, `animate`, `clearAnimation` ([#96](https://github.com/wechat-miniprogram/api-typings/issues/96))
214 | - 补齐 `App` `onUnhandledRejection` ([#99](https://github.com/wechat-miniprogram/api-typings/issues/99))
215 |
216 | ## 2020-01-07 v2.10.0-1
217 | - 修复接口错误 ([#95](https://github.com/wechat-miniprogram/api-typings/issues/95))
218 |
219 | ## 2020-01-07 v2.10.0
220 | - 同步 API 定义到基础库 2.10.0
221 |
222 | ## 2019-12-20 v2.9.4
223 | - 同步 API 定义到基础库 2.9.4
224 | - 修正一些接口错误 ([#88](https://github.com/wechat-miniprogram/api-typings/issues/88),[#89](https://github.com/wechat-miniprogram/api-typings/issues/89),[#91](https://github.com/wechat-miniprogram/api-typings/issues/91))
225 |
226 | ## 2019-12-06 v2.9.3
227 | - 同步 API 定义到基础库 2.9.3
228 | - 补齐 `Component` 纯数据字段 (`pureDataPattern`)
229 | - 支持 `Component` 的属性监听器使用 `string` 类型
230 |
231 | ## 2019-11-14 v2.9.2
232 | - 同步 API 定义到基础库 2.9.2
233 | - 补齐 `Behaviors` 中缺少的一些选项
234 |
235 | ## 2019-11-06 v2.9.1
236 | - 同步 API 定义到基础库 2.9.1
237 |
238 | ## 2019-10-23 v2.9.0
239 | - 同步 API 定义到基础库 2.9.0
240 |
241 | ## 2019-10-10 v2.8.3-1
242 | - 修复注释文档中不可用的链接
243 | - 组件实例类型支持 `Partial` 的自定义方法 ([用例](https://github.com/wechat-miniprogram/api-typings/blob/master/test/issue.test.ts#L170-L185))
244 |
245 | ## 2019-09-19 v2.8.3
246 | - 同步 API 定义到基础库 2.8.3
247 | - `getApp` 支持范型 ([#77](https://github.com/wechat-miniprogram/api-typings/issues/77))
248 | - 修正一些接口错误 ([#73](https://github.com/wechat-miniprogram/api-typings/issues/73), [#75](https://github.com/wechat-miniprogram/api-typings/issues/75), [#79](https://github.com/wechat-miniprogram/api-typings/issues/79))
249 | - 补齐 `require`, `exports`, `module.exports` 定义,以支持在没有 `@types/node` 下编译
250 |
251 | ## 2019-09-10 v2.8.2
252 | - 同步 API 定义到基础库 2.8.2
253 | - 加强了参数为可选值的方法参数类型定义和注释 (如 `FileSystemManager.appendFileSync` 的 `encoding`)
254 |
255 | ## 2019-08-30 v2.8.1
256 | - 同步 API 定义到基础库 2.8.1
257 | - 修复了部分最低基础库显示为 `[object Object]` 的问题
258 |
259 | ## 2019-08-20 v2.8.0-2
260 |
261 | - 将 `object` 改为 `Record`,以允许任意属性和方法
262 | - 自定义组件属性构造器为 `ObjectConstructor` 时,类型推导为 `Record` 而不是 `object`
263 | - 修正 `component` 参数的类型为页面或自定义组件实例
264 | - 补齐 `console: WechatMiniprogram.Console` 全局变量
265 | - 修正一些其他的接口类型错误
266 |
267 | ## 2019-08-14 v2.8.0-1
268 |
269 | - 补齐 `styleIsolation` 到 `ComponentOption`
270 |
271 | ## 2019-08-14 v2.8.0
272 |
273 | - 同步 API 定义到基础库 2.8.0
274 | - 不再向全局暴露 `IAnyObject`,收回到命名空间 `WechatMiniprogram` 内
275 | - 对齐代码规范,使用 4 空格缩进,不再使用分号等
276 | - 小幅改动 behavior, component 和 page 的定义,使其对 data 和 properties 等的类型推断更准确
277 | - 修复了一些其他问题 ([#60](https://github.com/wechat-miniprogram/api-typings/issues/60), [#59](https://github.com/wechat-miniprogram/api-typings/issues/59), [#48](https://github.com/wechat-miniprogram/api-typings/issues/48), [#47](https://github.com/wechat-miniprogram/api-typings/issues/47), [#45](https://github.com/wechat-miniprogram/api-typings/issues/45), [#33](https://github.com/wechat-miniprogram/api-typings/issues/33), [#13](https://github.com/wechat-miniprogram/api-typings/issues/13))
278 |
279 | ## 2019-08-08 v2.7.7-2
280 |
281 | - 补齐了部分接口 fail 回调的错误码 ([#51](https://github.com/wechat-miniprogram/api-typings/issues/51))
282 |
283 | ## 2019-08-06 v2.7.7-1
284 |
285 | - 重写了 page, component 和 behavior 的定义,替换原来不完整的定义,使其更全面,更准确 ([#46](https://github.com/wechat-miniprogram/api-typings/issues/46), [#40](https://github.com/wechat-miniprogram/api-typings/issues/40), [#30](https://github.com/wechat-miniprogram/api-typings/issues/30), [#28](https://github.com/wechat-miniprogram/api-typings/issues/28), [#27](https://github.com/wechat-miniprogram/api-typings/issues/27))
286 |
287 | ## 2019-07-31 v2.7.7
288 |
289 | - 同步 API 定义到基础库 2.7.7
290 | - 将命名空间从 `Wx` 更改为更正式的 `WechatMiniprogram`,这是一个 **破坏性改动**,原本字面上引用了 `Wx` 命名空间的代码可能失效
291 | - 修复了云开发的定义无法使用的问题 ([#25](https://github.com/wechat-miniprogram/api-typings/issues/25), [#32](https://github.com/wechat-miniprogram/api-typings/issues/32), [#42](https://github.com/wechat-miniprogram/api-typings/issues/42))
292 | - 修复了一些其它问题 ([#11](https://github.com/wechat-miniprogram/api-typings/issues/11), [#35](https://github.com/wechat-miniprogram/api-typings/issues/35), [#43](https://github.com/wechat-miniprogram/api-typings/issues/43))
293 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README-en.md:
--------------------------------------------------------------------------------
1 | # Wechat Mini Program API Typings
2 |
3 | > [中文版本](./README.md)
4 |
5 | [](https://www.npmjs.com/package/@types/wechat-miniprogram)
6 | [](https://www.npmjs.com/package/miniprogram-api-typings)
7 | [](https://github.com/wechat-miniprogram/api-typings)
8 | [](https://github.com/wechat-miniprogram/api-typings/actions/workflows/test.yml)
9 |
10 | Type definitions for APIs of Wechat Mini Program in TypeScript
11 |
12 | ## Install
13 |
14 | ### By standalone npm package
15 |
16 | ```bash
17 | npm install miniprogram-api-typings
18 | ```
19 |
20 | Manually import it after installed:
21 |
22 | - `import 'miniprogram-api-typings';`
23 |
24 | Or specify types in typescript config:
25 |
26 | - Specify `types: ["miniprogram-api-typings"]` in `tsconfig.json`
27 |
28 | Or reference by [Triple-Slash Directives](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html):
29 |
30 | - `/// `
31 |
32 | or:
33 |
34 | ### By DefinitelyTyped
35 |
36 | ```bash
37 | npm install @types/wechat-miniprogram
38 | ```
39 |
40 | ## Changelog
41 |
42 | See [CHANGELOG.md](https://github.com/wechat-miniprogram/api-typings/blob/master/CHANGELOG.md) (Chinese only)
43 |
44 | ## Contribution
45 |
46 | Definitions of Wechat APIs (`lib.wx.api.d.ts`) are auto-generated together with our [documentations](https://developers.weixin.qq.com/miniprogram/en/dev/api/), therefore PRs including that file will __not__ be merged. If you found some APIs defined wrongly, create an issue instead.
47 |
48 | Both PR and issue are welcomed for definitions of pages (`Page`), custom components (`Component`) and other else, since they are written manually. Help us improve this definition if you have any bug reports or suggestions! Thanks for contributing!
49 |
50 | ### Contributors
51 |
52 | - [Baran](https://github.com/baranwang)
53 | - [MinLiang Zeng](https://github.com/zenml/)
54 | - [Garfield Lee](https://github.com/Garfield550)
55 | - [Mr.Hope](https://github.com/Mister-Hope)
56 | - [chs97](https://github.com/chs97)
57 | - [Jelf](https://github.com/okxiaoliang4)
58 | - [xieyuhang](https://github.com/haiya6)
59 | - [苏杰豪](https://github.com/Megasu)
60 | - [Yang Mingshan](https://github.com/yangmingshan)
61 | - [lvzl](https://github.com/lv-z-l)
62 |
63 | ### Automated tests
64 |
65 | We use [`tsd`](https://github.com/SamVerschueren/tsd) to check if this definition is working properly. All test cases are under folder `test`.
66 |
67 | To perform an automated test, clone this repo, `npm install --save-dev` and `npm test`.
68 |
69 | If you have test case that fails the test, an issue or PR will be great. Strong test case that passes are also welcomed.
70 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 微信小程序定义文件
2 |
3 | > [English version](./README-en.md)
4 |
5 | [](https://www.npmjs.com/package/@types/wechat-miniprogram)
6 | [](https://www.npmjs.com/package/miniprogram-api-typings)
7 | [](https://github.com/wechat-miniprogram/api-typings)
8 | [](https://github.com/wechat-miniprogram/api-typings/actions/workflows/test.yml)
9 |
10 | 微信小程序 API 的 TypeScript 类型定义文件
11 |
12 | ## 安装
13 |
14 | ### 通过独立 npm 包
15 |
16 | ```bash
17 | npm install miniprogram-api-typings
18 | ```
19 |
20 | 安装后手动导入:
21 |
22 | - `import 'miniprogram-api-typings';`
23 |
24 | 或者在 ts 配置中指定:
25 |
26 | - 在 `tsconfig.json` 中指定 `types: ["miniprogram-api-typings"]`
27 |
28 | 或者通过 [三斜杠指令](https://www.tslang.cn/docs/handbook/triple-slash-directives.html) 引用:
29 |
30 | - `/// `
31 |
32 | 或:
33 |
34 | ### 通过 DefinitelyTyped
35 |
36 | ```bash
37 | npm install @types/wechat-miniprogram
38 | ```
39 |
40 | ## 更新日志
41 |
42 | 参考 [CHANGELOG.md](https://github.com/wechat-miniprogram/api-typings/blob/master/CHANGELOG.md)
43 |
44 | ## 贡献
45 |
46 | API 的定义文件(`lib.wx.api.d.ts`)是随 [文档](https://developers.weixin.qq.com/miniprogram/dev/api/) 一起自动生成的,如果发现了 API 接口的定义错误,请提一个 issue 给我们,关于 API 的 PR 将 __不会__ 被接受。
47 |
48 | 如果有针对页面(`Page`)、自定义组件(`Component`)等接口的 bug 和建议,欢迎 PR 或提一个 issue 给我们。非常感谢!
49 |
50 | ### 贡献者
51 |
52 | - [Baran](https://github.com/baranwang)
53 | - [MinLiang Zeng](https://github.com/zenml/)
54 | - [Garfield Lee](https://github.com/Garfield550)
55 | - [Mr.Hope](https://github.com/Mister-Hope)
56 | - [chs97](https://github.com/chs97)
57 | - [Jelf](https://github.com/okxiaoliang4)
58 | - [xieyuhang](https://github.com/haiya6)
59 | - [苏杰豪](https://github.com/Megasu)
60 | - [Yang Mingshan](https://github.com/yangmingshan)
61 | - [lvzl](https://github.com/lv-z-l)
62 |
63 | ### 测试
64 |
65 | 本定义文件使用 [`tsd`](https://github.com/SamVerschueren/tsd) 进行测试,所有的测试样例放在 `test` 目录下。
66 |
67 | 想执行测试的话,克隆本项目并完成 `npm install --save-dev` 后执行 `npm test` 即可。
68 |
69 | 如果您发现了不能通过自动化测试的测试样例,可以提交 PR 或者提一个 issue。当然,能通过自动化测试的强有力的测试样例也是欢迎的。
70 |
--------------------------------------------------------------------------------
/index.d.ts:
--------------------------------------------------------------------------------
1 | ///
2 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "miniprogram-api-typings",
3 | "version": "4.0.7",
4 | "description": "Type definitions for APIs of Wechat Mini Program in TypeScript",
5 | "main": "./index.d.ts",
6 | "types": "./index.d.ts",
7 | "scripts": {
8 | "test": "npm run tsd && npm run eslint",
9 | "tsd": "tsd",
10 | "eslint": "eslint --config .eslintrc.js types/**/*.ts"
11 | },
12 | "repository": {
13 | "type": "git",
14 | "url": "git+https://github.com/wechat-miniprogram/api-typings.git"
15 | },
16 | "author": "Wechat Miniprogram ",
17 | "license": "MIT",
18 | "bugs": {
19 | "url": "https://github.com/wechat-miniprogram/api-typings/issues"
20 | },
21 | "homepage": "https://github.com/wechat-miniprogram/api-typings#readme",
22 | "devDependencies": {
23 | "@typescript-eslint/eslint-plugin": "^6.21.0",
24 | "@typescript-eslint/parser": "^6.21.0",
25 | "eslint": "^8.57.1",
26 | "tsd": "^0.32.0",
27 | "typescript": "^5.2.2 <5.4.0"
28 | },
29 | "tsd": {
30 | "directory": "test"
31 | },
32 | "files": [
33 | "LICENSE",
34 | "CHANGELOG.md",
35 | "README.md",
36 | "README-en.md",
37 | "index.d.ts",
38 | "typings.json",
39 | "types/"
40 | ]
41 | }
42 |
--------------------------------------------------------------------------------
/test/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | rules: {
3 | indent: ['error', 2],
4 | },
5 | }
--------------------------------------------------------------------------------
/test/api-extend.test.d.ts:
--------------------------------------------------------------------------------
1 | declare namespace WechatMiniprogram {
2 | interface Wx {
3 | customMethod(a: number): string
4 | }
5 | interface CanvasContext {
6 | customMethod(): void
7 | }
8 | interface SocketTask {
9 | readyState: number
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/test/api-extend.test.ts:
--------------------------------------------------------------------------------
1 | ///
2 |
3 | import { expectType } from 'tsd'
4 |
5 | {
6 | expectType(wx.customMethod(1))
7 | const ctx = wx.createCanvasContext('test')
8 | expectType(ctx.customMethod())
9 | const task = wx.connectSocket({ url: '' })
10 | task.readyState = 1
11 | }
12 |
--------------------------------------------------------------------------------
/test/api-promisify.test.ts:
--------------------------------------------------------------------------------
1 | import { expectType } from 'tsd'
2 |
3 | // call with callback
4 | wx.chooseImage({
5 | success(res) {
6 | expectType(res)
7 | },
8 | })
9 | wx.canvasToTempFilePath({
10 | canvasId: '',
11 | success(res) {
12 | expectType(
13 | res,
14 | )
15 | },
16 | })
17 | wx.stopAccelerometer({
18 | fail(res) {
19 | expectType(res)
20 | },
21 | })
22 | wx.getClipboardData({
23 | success(res) {
24 | expectType(res)
25 | },
26 | })
27 | wx.stopCompass({
28 | complete(res) {
29 | expectType(res)
30 | },
31 | })
32 | wx.addPhoneContact({
33 | firstName: '',
34 | complete(res) {
35 | expectType(res)
36 | },
37 | })
38 | wx.startLocalServiceDiscovery({
39 | serviceType: '',
40 | success(res) {
41 | expectType(res)
42 | },
43 | })
44 | wx.getSystemInfo({
45 | success(res) {
46 | expectType(res)
47 | },
48 | })
49 | wx.chooseLocation({
50 | success(res) {
51 | expectType(res)
52 | },
53 | })
54 | wx.previewImage({
55 | urls: [],
56 | success(res) {
57 | expectType(res)
58 | },
59 | })
60 | wx.saveVideoToPhotosAlbum({
61 | filePath: '',
62 | success(res) {
63 | expectType(res)
64 | },
65 | })
66 |
67 | wx.createBLEConnection({
68 | deviceId: '',
69 | success(res) {
70 | expectType(res)
71 | },
72 | })
73 |
74 | wx.startBluetoothDevicesDiscovery({
75 | success(res) {
76 | expectType(res)
77 | },
78 | })
79 | wx.hideShareMenu({
80 | success(res) {
81 | expectType(res)
82 | },
83 | })
84 |
85 | wx.checkIsSupportSoterAuthentication({
86 | success(res) {
87 | expectType<
88 | WechatMiniprogram.CheckIsSupportSoterAuthenticationSuccessCallbackResult
89 | >(res)
90 | },
91 | })
92 | wx.navigateBack({
93 | success(res) {
94 | expectType(res)
95 | },
96 | })
97 |
98 | // call with Promise.prototype.then
99 | wx.chooseImage({}).then(res => {
100 | expectType(res)
101 | })
102 | wx.canvasToTempFilePath({
103 | canvasId: '',
104 | }).then(res => {
105 | expectType(res)
106 | })
107 | wx.stopAccelerometer().then(res => {
108 | expectType(res)
109 | })
110 | wx.getClipboardData().then(res => {
111 | expectType(res)
112 | })
113 | wx.stopCompass().then(res => {
114 | expectType(res)
115 | })
116 | wx.addPhoneContact({
117 | firstName: '',
118 | }).then(res => {
119 | expectType(res)
120 | })
121 | wx.startLocalServiceDiscovery({ serviceType: '' }).then(res => {
122 | expectType(res)
123 | })
124 | wx.getSystemInfo().then(res => {
125 | expectType(res)
126 | })
127 | wx.chooseLocation({}).then(res => {
128 | expectType(res)
129 | })
130 | wx.previewImage({ urls: [] }).then(res => {
131 | expectType(res)
132 | })
133 | wx.saveVideoToPhotosAlbum({ filePath: '' }).then(res => {
134 | expectType(res)
135 | })
136 | wx.createBLEConnection({ deviceId: '' }).then(res => {
137 | expectType(res)
138 | })
139 | wx.startBluetoothDevicesDiscovery({}).then(res => {
140 | expectType(res)
141 | })
142 | wx.hideShareMenu().then(res => {
143 | expectType(res)
144 | })
145 | wx.checkIsSupportSoterAuthentication().then(res => {
146 | expectType<
147 | WechatMiniprogram.CheckIsSupportSoterAuthenticationSuccessCallbackResult
148 | >(res)
149 | })
150 | wx.navigateBack().then(res => {
151 | expectType(res)
152 | })
153 |
154 | // call with await
155 | async () => {
156 | expectType(
157 | await wx.chooseImage({}),
158 | )
159 | expectType(
160 | await wx.canvasToTempFilePath({ canvasId: '' }),
161 | )
162 | expectType(
163 | await wx.stopAccelerometer(),
164 | )
165 | expectType(
166 | await wx.getClipboardData(),
167 | )
168 | expectType(await wx.stopCompass())
169 | expectType(
170 | await wx.addPhoneContact({ firstName: '' }),
171 | )
172 | expectType(
173 | await wx.startLocalServiceDiscovery({ serviceType: '' }),
174 | )
175 | expectType(
176 | await wx.getSystemInfo(),
177 | )
178 | expectType(
179 | await wx.chooseLocation({}),
180 | )
181 | expectType(
182 | await wx.previewImage({ urls: [] }),
183 | )
184 | expectType(
185 | await wx.saveVideoToPhotosAlbum({ filePath: '' }),
186 | )
187 | expectType(
188 | await wx.createBLEConnection({ deviceId: '' }),
189 | )
190 | expectType(
191 | await wx.startBluetoothDevicesDiscovery({}),
192 | )
193 | expectType(await wx.hideShareMenu())
194 | expectType<
195 | WechatMiniprogram.CheckIsSupportSoterAuthenticationSuccessCallbackResult
196 | >(await wx.checkIsSupportSoterAuthentication())
197 | expectType(await wx.navigateBack())
198 | }
199 |
--------------------------------------------------------------------------------
/test/api.test.ts:
--------------------------------------------------------------------------------
1 | import { expectType, expectNotType } from 'tsd'
2 |
3 | wx.request({
4 | url: 'https://www.baidu.com',
5 | success(res) {
6 | expectType(res.errMsg)
7 | expectType(res)
8 | },
9 | })
10 | wx.startBluetoothDevicesDiscovery({
11 | services: ['FEE7'],
12 | fail(res) {
13 | expectType(res.errCode)
14 | },
15 | })
16 | wx.authorize({
17 | scope: 'scope.record',
18 | })
19 | wx.navigateTo({
20 | url: '/pages/index/index',
21 | })
22 |
23 | {
24 | const ctx = wx.createCanvasContext('myCanvas')
25 | expectType(ctx)
26 | ctx.setFillStyle('red')
27 | ctx.fillRect(10, 10, 150, 75)
28 | ctx.draw()
29 | }
30 |
31 | getCurrentPages().map(p => p.options)
32 |
33 | const query = wx.createSelectorQuery()
34 | expectType(query)
35 | query.select('#a').boundingClientRect(res => {
36 | expectType(res.bottom)
37 | })
38 | query.selectViewport().scrollOffset(res => {
39 | expectType(res.scrollTop)
40 | })
41 | query.exec(res => {
42 | expectType(res)
43 | })
44 |
45 | Page({
46 | f() {
47 | wx.createSelectorQuery().in(this)
48 | },
49 | })
50 | Component({
51 | methods: {
52 | f() {
53 | wx.createSelectorQuery().in(this)
54 | },
55 | },
56 | })
57 |
58 | console.group('test')
59 | console.debug('console', 'debug')
60 | console.log('console', 'log')
61 | console.info('console', 'info')
62 | console.warn('console', 'warn')
63 | console.error('console', 'error')
64 | console.groupEnd()
65 |
66 | expectType(wx.env.USER_DATA_PATH)
67 |
68 | wx.getStorage({
69 | key: 'key',
70 | success(res) {
71 | expectType(res.data)
72 | }
73 | })
74 | wx.getStorage({ key: 'key' }).then((res) => {
75 | expectType(res.data)
76 | })
77 | wx.getStorage({
78 | key: 'key',
79 | success(res) {
80 | expectType(res.data)
81 | expectNotType(res.data)
82 | }
83 | })
84 |
85 | wx.request({
86 | url: 'https://developer.weixin.qq.com',
87 | success(res) {
88 | expectType(res.data)
89 | }
90 | })
91 |
92 | {
93 | const thisShouldBeAny = wx.getStorageSync('test')
94 | expectType(thisShouldBeAny)
95 | expectNotType(thisShouldBeAny)
96 | const thisShouldBeNumber = wx.getStorageSync('test')
97 | expectNotType(thisShouldBeNumber)
98 | expectType(thisShouldBeNumber)
99 | }
100 |
101 | wx.createSelectorQuery()
102 | .select('#canvas')
103 | .node(({ node }) => {
104 | const canvas = node as WechatMiniprogram.Canvas
105 | const ctx = canvas.getContext('2d')
106 |
107 | expectNotType(ctx)
108 | expectType(ctx.font)
109 | expectType<() => void>(ctx.save)
110 | })
111 | .exec()
112 |
--------------------------------------------------------------------------------
/test/app.test.ts:
--------------------------------------------------------------------------------
1 | import { expectType } from 'tsd'
2 |
3 | expectType(App({}))
4 |
5 | App({
6 | globalData: {
7 | userInfo: {} as WechatMiniprogram.UserInfo,
8 | },
9 | userInfoReadyCallback(userInfo: WechatMiniprogram.UserInfo) {
10 | userInfo.gender
11 | },
12 | onLaunch() {
13 | const logs: number[] = wx.getStorageSync('logs') || []
14 | logs.unshift(Date.now())
15 | wx.setStorageSync('logs', logs)
16 |
17 | wx.getSetting({
18 | success: res => {
19 | if (res.authSetting['scope.userInfo']) {
20 | wx.getUserInfo({
21 | success: res => {
22 | this.globalData.userInfo = res.userInfo
23 | if (this.userInfoReadyCallback) {
24 | this.userInfoReadyCallback(res.userInfo)
25 | }
26 | },
27 | })
28 | }
29 | },
30 | })
31 | },
32 | f(a: number) {
33 | return a.toFixed()
34 | },
35 | onError() {},
36 | onHide() {
37 | expectType(this.f(1))
38 | },
39 | onPageNotFound(e) {
40 | expectType(e.isEntryPage)
41 | },
42 | onUnhandledRejection({ reason, promise }) {
43 | expectType(reason)
44 | expectType>(promise)
45 | },
46 | onThemeChange(res) {
47 | expectType<'dark' | 'light'>(res.theme)
48 | },
49 | })
50 |
51 | expectType>>(getApp())
52 |
--------------------------------------------------------------------------------
/test/behavior.test.ts:
--------------------------------------------------------------------------------
1 | import { expectType } from 'tsd'
2 |
3 | expectType(Behavior({}))
4 |
5 | Behavior({
6 | behaviors: [],
7 | properties: {
8 | myProperty: {
9 | type: String,
10 | value: '',
11 | },
12 | myProperty2: String,
13 | min: {
14 | type: Number,
15 | value: 0,
16 | },
17 | max: {
18 | type: Number,
19 | value: 0,
20 | observer(newVal, oldVal) {
21 | expectType(newVal)
22 | expectType(oldVal.toExponential())
23 | },
24 | },
25 | },
26 | data: {
27 | text: 'init data',
28 | array: [{ msg: '1' }, { msg: '2' }],
29 | logs: [] as string[],
30 | },
31 | attached() {},
32 | methods: {
33 | myBehaviorMethod() {
34 | expectType(this.created)
35 | expectType(this.data.text)
36 | expectType(this.properties.text)
37 | expectType(this.data.max)
38 | expectType(this.properties.myProperty2)
39 | },
40 | },
41 | })
42 |
43 | Behavior({
44 | attached() {},
45 | pageLifetimes: {
46 | show() {},
47 | },
48 | })
49 |
--------------------------------------------------------------------------------
/test/component.test.ts:
--------------------------------------------------------------------------------
1 | import { expectType, expectError } from 'tsd'
2 |
3 | expectType(Component({}))
4 |
5 | Component({
6 | behaviors: [''],
7 |
8 | properties: {
9 | myProperty: {
10 | type: String,
11 | value: '',
12 | },
13 | freeType: {
14 | type: null,
15 | value: 'd',
16 | },
17 | myProperty2: String,
18 | min: {
19 | type: Number,
20 | value: 0,
21 | },
22 | test: {
23 | type: String,
24 | value: '',
25 | observer(n: string, o: string) {
26 | n.concat(o)
27 | },
28 | },
29 | max: {
30 | type: Number,
31 | value: 0,
32 | observer(newVal, oldVal) {
33 | expectType(newVal)
34 | expectType(oldVal)
35 | expectType(this.onMyButtonTap())
36 | expectType(this.data.max)
37 | },
38 | },
39 | lastLeaf: {
40 | type: Number,
41 | optionalTypes: [String, Object],
42 | value: 0,
43 | },
44 | },
45 |
46 | data: {
47 | text: 'init data',
48 | array: [{ msg: '1' }, { msg: '2' }],
49 | logs: [] as string[],
50 | },
51 |
52 | observers: {
53 | 'numberA, numberB'(numberA, numberB) {
54 | this.setData({
55 | sum: numberA + numberB,
56 | })
57 | },
58 | },
59 |
60 | lifetimes: {
61 | attached() {},
62 | moved() {},
63 | detached() {},
64 | error(err) {
65 | expectType(err)
66 | },
67 | },
68 |
69 | created() {},
70 |
71 | pageLifetimes: {
72 | show() {
73 | // is current component but not the page
74 | expectType(this.is)
75 | },
76 | },
77 |
78 | methods: {
79 | onMyButtonTap() {
80 | expectType(this.data.text)
81 | expectType(this.data.min.toFixed())
82 | this.triggerEvent(
83 | 'tap',
84 | { a: 1 },
85 | {
86 | bubbles: true,
87 | composed: true,
88 | capturePhase: true,
89 | },
90 | )
91 | },
92 | _myPrivateMethod() {
93 | this.setData({
94 | 'A[0].B': 'myPrivateData',
95 | })
96 | },
97 | _propertyChange(newVal: number, oldVal: number) {
98 | expectType(newVal)
99 | expectType(oldVal)
100 | },
101 | },
102 | export() {
103 | expectType(this.is)
104 | expectType(this.onMyButtonTap())
105 | return {}
106 | }
107 | })
108 |
109 | Component({
110 | methods: {
111 | f() {
112 | this.triggerEvent('someEvent', {
113 | a: 'test',
114 | b: 123,
115 | c: {
116 | t: 'test',
117 | },
118 | })
119 | },
120 | },
121 | })
122 |
123 | expectError(
124 | Component({
125 | custom: 1,
126 | methods: {
127 | f() {
128 | this.custom
129 | },
130 | },
131 | }),
132 | )
133 |
134 | interface Config {
135 | a: number
136 | }
137 |
138 | interface ConfigConstructor extends ObjectConstructor {
139 | new (value?: any): Config
140 | readonly prototype: Config
141 | }
142 |
143 | Component({
144 | properties: {
145 | config: {
146 | type: Object as ConfigConstructor,
147 | },
148 | },
149 | methods: {
150 | doc() {
151 | expectType>(this.data.config)
152 | },
153 | },
154 | options: {
155 | addGlobalClass: true,
156 | },
157 | })
158 |
159 | Component({
160 | properties: {
161 | n: Number,
162 | n2: {
163 | type: Number,
164 | value: 1,
165 | },
166 | s: String,
167 | a: Array,
168 | a2: {
169 | type: Array,
170 | value: [1, 2],
171 | },
172 | b: Boolean,
173 | o: Object,
174 | },
175 | methods: {
176 | f() {
177 | expectType(this.data.n)
178 | expectType(this.data.n2)
179 | expectType(this.data.s)
180 | expectType(this.data.a)
181 | expectType(this.data.a2)
182 | expectType(this.data.b)
183 | expectType>(this.data.o)
184 | expectType(this.data.a[0])
185 | expectType(this.data.o.prop)
186 | },
187 | },
188 | })
189 |
190 | Component({
191 | properties: {
192 | n: Number,
193 | n2: {
194 | type: Number,
195 | value: 1,
196 | },
197 | s: String,
198 | a: Array,
199 | a2: {
200 | type: Array,
201 | value: [1, 2],
202 | },
203 | b: Boolean,
204 | o: Object,
205 | o2: {
206 | type: Object,
207 | value: {} as Record,
208 | },
209 | },
210 | methods: {
211 | g() {
212 | const str = (1).toFixed(0)
213 | return str
214 | },
215 | f() {
216 | expectType(this.g())
217 | expectType(this.data.n)
218 | expectType(this.data.n2)
219 | expectType(this.data.s)
220 | expectType(this.data.a)
221 | expectType(this.data.a2)
222 | expectType(this.data.b)
223 | expectType>(this.data.o)
224 | expectType>(this.data.o2)
225 | expectType(this.data.o2.city)
226 | expectType(this.data.a[0])
227 | expectType(this.data.o.prop)
228 | },
229 | },
230 | })
231 |
232 | Component({
233 | properties: {
234 | n: {
235 | type: Number,
236 | value: 1,
237 | },
238 | a: {
239 | type: Array,
240 | value: [1, 2],
241 | },
242 | },
243 | methods: {
244 | f() {
245 | expectType(this.data.n)
246 | expectType(this.data.a)
247 | },
248 | },
249 | })
250 |
251 | Component({
252 | properties: {
253 | n: Number,
254 | a: Array,
255 | },
256 | methods: {
257 | f() {
258 | expectType(this.data.n)
259 | expectType(this.data.a)
260 | },
261 | },
262 | })
263 |
264 | expectError(
265 | Component({
266 | data: {
267 | a: 1,
268 | },
269 | methods: {
270 | someMethod() {
271 | this.setData({
272 | a: '',
273 | })
274 | },
275 | },
276 | }),
277 | )
278 |
279 | Component({
280 | options: {
281 | pureDataPattern: /^_/,
282 | },
283 | data: {
284 | a: true, // 普通数据字段
285 | _b: true, // 纯数据字段
286 | },
287 | methods: {
288 | myMethod() {
289 | this.data._b // 纯数据字段可以在 this.data 中获取
290 | this.setData({
291 | c: true, // 普通数据字段
292 | _d: true, // 纯数据字段
293 | })
294 | },
295 | },
296 | })
297 |
298 | Component({
299 | properties: {
300 | a: {
301 | type: Number,
302 | observer: 'onAChange',
303 | value: 1,
304 | },
305 | },
306 | methods: {
307 | onAChange() {},
308 | },
309 | })
310 |
311 | Component({
312 | methods: {
313 | animate() {
314 | this.animate(
315 | '#container',
316 | [
317 | { opacity: 1.0, rotate: 0, backgroundColor: '#FF0000' },
318 | { opacity: 0.5, rotate: 45, backgroundColor: '#00FF00' },
319 | { opacity: 0.0, rotate: 90, backgroundColor: '#FF0000' },
320 | ],
321 | 5000,
322 | () => {
323 | this.clearAnimation(
324 | '#container',
325 | { opacity: true, rotate: true },
326 | function() {
327 | console.log('清除了#container上的opacity和rotate属性')
328 | },
329 | )
330 | },
331 | )
332 |
333 | this.animate(
334 | '.block',
335 | [
336 | { scale: [1, 1], rotate: 0, ease: 'ease-out' },
337 | { scale: [1.5, 1.5], rotate: 45, ease: 'ease-in', offset: 0.9 },
338 | { scale: [2, 2], rotate: 90 },
339 | ],
340 | 5000,
341 | () => {
342 | this.clearAnimation('.block', function() {
343 | console.log('清除了.block上的所有动画属性')
344 | })
345 | },
346 | )
347 | },
348 | },
349 | })
350 |
351 | Component({
352 | methods: {
353 | animate() {
354 | this.animate(
355 | '.avatar',
356 | [
357 | {
358 | borderRadius: '0',
359 | borderColor: 'red',
360 | transform: 'scale(1) translateY(-20px)',
361 | offset: 0,
362 | },
363 | {
364 | borderRadius: '25%',
365 | borderColor: 'blue',
366 | transform: 'scale(.65) translateY(-20px)',
367 | offset: 0.5,
368 | },
369 | {
370 | borderRadius: '50%',
371 | borderColor: 'blue',
372 | transform: 'scale(.3) translateY(-20px)',
373 | offset: 1,
374 | },
375 | ],
376 | 2000,
377 | {
378 | scrollSource: '#scroller',
379 | timeRange: 2000,
380 | startScrollOffset: 0,
381 | endScrollOffset: 85,
382 | },
383 | )
384 |
385 | this.animate(
386 | '.search_input',
387 | [
388 | {
389 | opacity: '0',
390 | width: '0%',
391 | },
392 | {
393 | opacity: '1',
394 | width: '100%',
395 | },
396 | ],
397 | 1000,
398 | {
399 | scrollSource: '#scroller',
400 | timeRange: 1000,
401 | startScrollOffset: 120,
402 | endScrollOffset: 252,
403 | },
404 | )
405 | },
406 | },
407 | })
408 |
409 | Component({
410 | methods: {
411 | test() {
412 | const channel = this.getOpenerEventChannel()
413 | expectType(channel)
414 | channel.emit('test', {})
415 | channel.on('xxx', () => {})
416 | expectError(channel.emit(1, 2))
417 | },
418 | },
419 | })
420 |
421 | Component<{}, {}, { fn(): void }, []>({
422 | methods: {
423 | fn() {
424 | expectError(this.notExists)
425 | },
426 | },
427 | })
428 |
429 | {
430 | const data = {
431 | a: 1,
432 | b: '',
433 | }
434 | const properties = {
435 | c: String,
436 | d: {
437 | type: Number,
438 | value: 4,
439 | },
440 | }
441 | Component<
442 | typeof data,
443 | typeof properties,
444 | /* methods= */{ fn(): string },
445 | /* behaviors= */ [],
446 | /* customProperties= */{},
447 | /* isPage= */true
448 | >({
449 | data,
450 | properties,
451 | methods: {
452 | onLoad(q) {
453 | expectType(Object.keys(q))
454 | },
455 | fn() {
456 | expectType<() => (void | Promise)>(this.onShow)
457 | expectError(this.notExists)
458 | return 'test'
459 | },
460 | },
461 | })
462 | }
463 |
464 | {
465 | Component({
466 | properties: {
467 | b: {
468 | type: Boolean,
469 | value: true,
470 | }
471 | },
472 | methods: {
473 | test() {
474 | expectType(this.data.b)
475 | }
476 | }
477 | })
478 | }
479 |
480 | {
481 | Component({
482 | attached() {
483 | this.setUpdatePerformanceListener({withDataPaths: true}, (res) => {
484 | expectType(res.dataPaths)
485 | expectType(res.updateProcessId)
486 | })
487 | this.setUpdatePerformanceListener({}, res => {
488 | expectType(res.dataPaths)
489 | expectType(res.updateProcessId)
490 | })
491 | this.setUpdatePerformanceListener({})
492 | }
493 | })
494 | }
495 |
496 | {
497 | type CustomProperties = {
498 | customProp: string
499 | }
500 | Component<{}, {}, {}, [], CustomProperties>({
501 | lifetimes: {
502 | created() {
503 | this.customProp = 'customProp'
504 | }
505 | }
506 | })
507 | }
508 |
--------------------------------------------------------------------------------
/test/computed.test.d.ts:
--------------------------------------------------------------------------------
1 | declare namespace WechatMiniprogram {
2 | namespace Component {
3 | type ComputedOption = Record any>
4 | interface Computed {
5 | computed: C
6 | }
7 | type ComputedOptionToData = {
8 | [K in keyof C]: C[K] extends () => infer R ? R : any
9 | }
10 |
11 | type ComputedInstance<
12 | D extends DataOption,
13 | P extends PropertyOption,
14 | M extends MethodOption,
15 | C extends ComputedOption,
16 | CustomProperty extends IAnyObject = Record,
17 | > = Instance & { data: ComputedOptionToData }
18 | type ComputedOptions<
19 | D extends DataOption,
20 | P extends PropertyOption,
21 | M extends MethodOption,
22 | C extends ComputedOption,
23 | CustomProperty extends IAnyObject = Record,
24 | > = Partial> &
25 | ThisType> &
26 | Options
27 | interface ComputedConstructor {
28 | <
29 | D extends DataOption,
30 | P extends PropertyOption,
31 | M extends MethodOption,
32 | C extends ComputedOption,
33 | CustomProperty extends IAnyObject = Record,
34 | >(
35 | options: ComputedOptions,
36 | ): string
37 | }
38 | }
39 | }
40 | declare type ComputedComponent = WechatMiniprogram.Component.ComputedConstructor
41 | declare type IAnyObject = WechatMiniprogram.IAnyObject
42 | declare type ComputedOptions = WechatMiniprogram.Component.ComputedOptions<
43 | IAnyObject,
44 | IAnyObject,
45 | IAnyObject,
46 | IAnyObject,
47 | IAnyObject
48 | >
49 |
--------------------------------------------------------------------------------
/test/computed.test.ts:
--------------------------------------------------------------------------------
1 | import { expectType } from 'tsd'
2 |
3 | ///
4 |
5 | const ComputedComponent = Component as ComputedComponent
6 | ComputedComponent({
7 | properties: {
8 | a: String,
9 | },
10 | data: {
11 | b: {
12 | c: 'test',
13 | },
14 | },
15 | computed: {
16 | d() {
17 | return 'test'
18 | },
19 | },
20 | methods: {
21 | f() {
22 | expectType(this.data.d)
23 | },
24 | },
25 | })
26 |
27 | type IMethods = {
28 | _setData: WechatMiniprogram.Component.InstanceMethods<{}>['setData']
29 | }
30 | type ICustomProperty = {
31 | _originalSetData: WechatMiniprogram.Component.InstanceMethods<{}>['setData']
32 | }
33 | Behavior<{}, {}, IMethods, [], ICustomProperty>({
34 | lifetimes: {
35 | created() {
36 | this._originalSetData = this.setData
37 | this.setData = this._setData
38 | },
39 | },
40 | definitionFilter(defFields: ComputedOptions) {
41 | const computed = defFields.computed || {}
42 | const computedKeys = Object.keys(computed)
43 | const computedCache: Record = {}
44 |
45 | const calcComputed = (scope: typeof defFields, insertToData?: boolean) => {
46 | const needUpdate: Record = {}
47 | const data = (defFields.data = defFields.data || {})
48 |
49 | for (const key of computedKeys) {
50 | const value = computed[key].call(scope)
51 | if (computedCache[key] !== value) {
52 | needUpdate[key] = computedCache[key] = value
53 | }
54 | if (insertToData) data[key] = needUpdate[key]
55 | }
56 |
57 | return needUpdate
58 | }
59 |
60 | defFields.methods = defFields.methods || {}
61 | defFields.methods._setData = function(
62 | data: Record,
63 | callback?: () => void,
64 | ) {
65 | const originalSetData = this._originalSetData
66 | originalSetData.call(this, data, callback)
67 | const needUpdate = calcComputed(this)
68 | originalSetData.call(this, needUpdate)
69 | }
70 |
71 | calcComputed(defFields, true)
72 | },
73 | })
74 |
--------------------------------------------------------------------------------
/test/index.d.ts:
--------------------------------------------------------------------------------
1 | ///
2 |
--------------------------------------------------------------------------------
/test/issue.test.ts:
--------------------------------------------------------------------------------
1 | import { expectError, expectType } from 'tsd'
2 |
3 | // https://github.com/wechat-miniprogram/api-typings/issues/11
4 | expectType(wx.env.USER_DATA_PATH)
5 |
6 | // https://github.com/wechat-miniprogram/api-typings/issues/13
7 | const ctx = wx.createCanvasContext('myCanvas')
8 | ctx.drawImage('', 1, 1)
9 | ctx.drawImage('', 0, 0, 150, 100)
10 | ctx.drawImage('', 0, 0, 0, 0, 0, 0, 0, 0)
11 |
12 | // https://github.com/wechat-miniprogram/api-typings/issues/15
13 | wx.getSetting({
14 | success(res) {
15 | expectType(res.authSetting['scope.userInfo'])
16 | },
17 | })
18 |
19 | // https://github.com/wechat-miniprogram/api-typings/issues/25
20 | // https://github.com/wechat-miniprogram/api-typings/issues/32
21 | // https://github.com/wechat-miniprogram/api-typings/issues/42
22 | wx.cloud.callFunction({
23 | name: 'add',
24 | data: {
25 | x: 1,
26 | y: 2,
27 | },
28 | success: res => {
29 | expectType(res.errMsg)
30 | },
31 | fail: err => {
32 | expectType(err.errMsg)
33 | },
34 | })
35 |
36 | wx.cloud.deleteFile({
37 | fileList: ['a7xzcb'],
38 | success: res => {
39 | expectType(res.fileList)
40 | },
41 | })
42 |
43 | // https://github.com/wechat-miniprogram/api-typings/issues/33
44 | wx.addCard({
45 | cardList: [
46 | {
47 | cardId: '',
48 | cardExt: '{"code": "", "openid": "", "timestamp": "", "signature":""}',
49 | },
50 | {
51 | cardId: '',
52 | cardExt: '{"code": "", "openid": "", "timestamp": "", "signature":""}',
53 | },
54 | ],
55 | success(res) {
56 | expectType(res.cardList)
57 | },
58 | })
59 |
60 | // https://github.com/wechat-miniprogram/api-typings/issues/35
61 | wx.chooseMessageFile({
62 | count: 10,
63 | type: 'image',
64 | success(res) {
65 | expectType(res.tempFiles)
66 | },
67 | })
68 |
69 | // https://github.com/wechat-miniprogram/api-typings/issues/43
70 | wx.canvasGetImageData({
71 | canvasId: 'myCanvas',
72 | x: 0,
73 | y: 0,
74 | width: 100,
75 | height: 100,
76 | success(res) {
77 | expectType(res.width)
78 | expectType(res.height)
79 | expectType(res.data)
80 | expectType(res.data.length)
81 | },
82 | })
83 |
84 | // https://github.com/wechat-miniprogram/api-typings/issues/45
85 | wx.onGyroscopeChange(res => {
86 | expectType(res.x)
87 | expectType(res.y)
88 | expectType(res.z)
89 | })
90 |
91 | // https://github.com/wechat-miniprogram/api-typings/issues/47
92 | wx.reportAnalytics('purchase', {
93 | price: 120,
94 | color: 'red',
95 | })
96 |
97 | // https://github.com/wechat-miniprogram/api-typings/issues/48
98 | wx.navigateBack()
99 | wx.navigateBack({})
100 | wx.navigateBack({ delta: 1 })
101 | wx.setTabBarStyle()
102 | wx.setTabBarStyle({})
103 | wx.setTabBarStyle({ color: '#111111' })
104 | wx.setBackgroundColor({ backgroundColor: '#111111' })
105 | wx.setNavigationBarColor({
106 | frontColor: '#ffffff',
107 | backgroundColor: '#123456',
108 | })
109 |
110 | // https://github.com/wechat-miniprogram/api-typings/issues/59
111 | wx.login({
112 | success(res) {
113 | expectType(res.errMsg)
114 | },
115 | })
116 |
117 | // https://github.com/wechat-miniprogram/api-typings/issues/60
118 | wx.loadFontFace({
119 | source: '',
120 | family: 'font',
121 | success(res) {
122 | expectType(res.status)
123 | },
124 | fail(res) {
125 | expectType(res.status)
126 | },
127 | complete(res) {
128 | expectType(res.status)
129 | },
130 | })
131 |
132 | // https://github.com/wechat-miniprogram/api-typings/issues/65
133 | wx.request({
134 | url: '',
135 | success(res) {
136 | const data: WechatMiniprogram.IAnyObject = { res }
137 | expectType(data.customData)
138 | },
139 | })
140 | interface IResponse {
141 | customData: string
142 | }
143 | wx.request({
144 | url: '',
145 | success(res) {
146 | const data = res.data as IResponse
147 | expectType(data.customData)
148 | },
149 | })
150 |
151 | // https://github.com/wechat-miniprogram/api-typings/issues/73
152 | {
153 | const task = wx.connectSocket({ url: '' })
154 | task.onClose(res => {
155 | expectType(res.code)
156 | expectType(res.reason)
157 | })
158 | }
159 |
160 | // https://github.com/wechat-miniprogram/api-typings/issues/75
161 | {
162 | wx.onBluetoothDeviceFound(res => {
163 | res.devices.forEach(device => {
164 | expectType(device.name)
165 | expectType(device.deviceId)
166 | })
167 | })
168 | }
169 |
170 | // https://github.com/wechat-miniprogram/api-typings/issues/82
171 | {
172 | interface IDialogMethod
173 | extends Partial {
174 | f?: () => string
175 | g: () => void
176 | }
177 | type Dialog = WechatMiniprogram.Component.Instance<{}, {}, IDialogMethod, []>
178 | Page({
179 | f() {
180 | const comp = this.selectComponent('#comp') as Dialog
181 | expectType<(() => string) | undefined>(comp.f)
182 | if (comp.f) expectType(comp.f())
183 | comp.g()
184 | },
185 | })
186 | }
187 |
188 | // https://github.com/wechat-miniprogram/api-typings/issues/85
189 | {
190 | Behavior({
191 | observers: {
192 | value(v) {
193 | expectType(v)
194 | },
195 | },
196 | })
197 | }
198 |
199 | // https://github.com/wechat-miniprogram/api-typings/issues/87
200 | import WX = WechatMiniprogram
201 | {
202 | type ILoginResult = WX.LoginSuccessCallbackResult
203 | const t: ILoginResult = { code: '', errMsg: '' }
204 | expectType(t)
205 | }
206 |
207 | // https://github.com/wechat-miniprogram/api-typings/issues/88
208 | {
209 | wx.createSelectorQuery()
210 | .select('#canvas')
211 | .fields({ node: true })
212 | .exec(res => {
213 | wx.canvasToTempFilePath({ canvas: res[0].node })
214 | wx.canvasToTempFilePath({ canvas: res[0].node, quality: 0.5 })
215 | })
216 | wx.canvasToTempFilePath({ canvasId: '' })
217 | }
218 |
219 | // https://github.com/wechat-miniprogram/api-typings/issues/89
220 | {
221 | const udp = wx.createUDPSocket()
222 | const port = udp.bind()
223 | expectType(port)
224 | }
225 |
226 | // https://github.com/wechat-miniprogram/api-typings/issues/91
227 | {
228 | expectType>(wx.getExtConfigSync())
229 | wx.getExtConfig({
230 | success(res) {
231 | expectType>(res.extConfig)
232 | },
233 | })
234 | }
235 |
236 | // https://github.com/wechat-miniprogram/api-typings/issues/95
237 | {
238 | Page({
239 | ctx: {} as WechatMiniprogram.CameraContext,
240 | onLoad() {
241 | this.ctx = wx.createCameraContext()
242 | },
243 | takePhoto() {
244 | this.ctx.takePhoto({
245 | quality: 'high',
246 | success: res => {
247 | expectType(res.tempImagePath)
248 | this.setData({
249 | src: res.tempImagePath,
250 | })
251 | },
252 | })
253 | },
254 | startRecord() {
255 | this.ctx.startRecord({
256 | success: res => {
257 | expectType(res.errMsg)
258 | console.log('startRecord')
259 | },
260 | })
261 | },
262 | stopRecord() {
263 | this.ctx.stopRecord({
264 | success: res => {
265 | expectType(res.tempThumbPath)
266 | expectType(res.tempVideoPath)
267 | this.setData({
268 | src: res.tempThumbPath,
269 | videoSrc: res.tempVideoPath,
270 | })
271 | },
272 | })
273 | },
274 | })
275 | }
276 |
277 | // https://github.com/wechat-miniprogram/api-typings/issues/133
278 | {
279 | type IData = {
280 | name: string
281 | }
282 | type IProperty = {
283 | id: typeof Number
284 | }
285 | type IMethod = {
286 | setJob(job: string): void
287 | }
288 | type IBehavior = []
289 | type ICustomInstanceProperty = {
290 | job: string
291 | }
292 | Component({
293 | properties: {
294 | id: Number,
295 | },
296 |
297 | data: {
298 | name: '',
299 | },
300 |
301 | methods: {
302 | setJob(job) {
303 | this.job = job
304 | expectType(this.job)
305 | },
306 | },
307 | })
308 | }
309 |
310 | // https://github.com/wechat-miniprogram/api-typings/issues/134
311 | {
312 | const ctx = wx.createMapContext('map')
313 | ctx.getRegion({
314 | success(res) {
315 | expectType(res.southwest.longitude)
316 | expectType(res.southwest.latitude)
317 | expectType(res.northeast.longitude)
318 | expectType(res.northeast.latitude)
319 | },
320 | })
321 | }
322 |
323 | // https://github.com/wechat-miniprogram/api-typings/issues/135
324 | {
325 | App({
326 | onThemeChange(res) {
327 | expectType<'light' | 'dark'>(res.theme)
328 | },
329 | })
330 | }
331 |
332 | // https://github.com/wechat-miniprogram/api-typings/issues/136
333 | {
334 | Page({
335 | onAddToFavorites(res) {
336 | if (res.webviewUrl) {
337 | // webview 页面返回 webviewUrl
338 | expectType(res.webviewUrl)
339 | }
340 | return {
341 | title: '自定义标题',
342 | imageUrl: 'http://demo.png',
343 | query: 'name=xxx&age=xxx',
344 | }
345 | },
346 | })
347 | }
348 |
349 | // https://github.com/wechat-miniprogram/api-typings/issues/154
350 | {
351 | wx.requestPayment({
352 | timeStamp: '',
353 | nonceStr: '',
354 | package: '',
355 | signType: 'MD5',
356 | paySign: '',
357 | })
358 | wx.requestPayment({
359 | timeStamp: '',
360 | nonceStr: '',
361 | package: '',
362 | signType: 'RSA',
363 | paySign: '',
364 | })
365 | }
366 |
367 | // https://github.com/wechat-miniprogram/api-typings/issues/157
368 | // Test case for #157 is removed since `wx.saveFile` is no longer supported
369 |
370 | // https://github.com/wechat-miniprogram/api-typings/issues/159
371 | {
372 | Page({
373 | onShareTimeline() {
374 | return {
375 | title: '',
376 | query: '',
377 | imageUrl: '',
378 | }
379 | },
380 | })
381 | }
382 |
383 | // https://github.com/wechat-miniprogram/api-typings/issues/161
384 | {
385 | Component({
386 | properties: {
387 | bar: {
388 | type: Object,
389 | value: { skuNum: 0 },
390 | observer () {}
391 | }
392 | },
393 | data: {
394 | foo: 123,
395 | },
396 | methods: {
397 | getDataOrPoperty() {
398 | return this.data.foo
399 | },
400 | test() {
401 | expectType<{
402 | skuNum: number
403 | }>(this.data.bar)
404 | expectType(this.getDataOrPoperty())
405 | },
406 | }
407 | })
408 | }
409 |
410 | // https://github.com/wechat-miniprogram/api-typings/issues/164
411 | {
412 | requirePlugin('myPlugin')
413 | requireMiniProgram()
414 | }
415 |
416 | // https://github.com/wechat-miniprogram/api-typings/issues/175
417 | {
418 | wx.requestSubscribeMessage({
419 | tmplIds: ['1', '2'],
420 | success: (res) => {
421 | expectType(res.errMsg)
422 | expectType(res.whatever)
423 | expectType(res.randomTemplateId)
424 | },
425 | })
426 | }
427 |
428 | // https://github.com/wechat-miniprogram/api-typings/issues/204
429 | {
430 | Page({
431 | test() {
432 | const observer = wx.createIntersectionObserver(this)
433 | observer.observe('', e => {
434 | expectType(e.id)
435 | expectType>(e.dataset)
436 | })
437 | }
438 | })
439 | }
440 |
441 | // https://github.com/wechat-miniprogram/api-typings/issues/189
442 | {
443 | const offscreenCanvas = wx.createOffscreenCanvas(800, 600)
444 | expectType(offscreenCanvas.height)
445 | expectType(offscreenCanvas.width)
446 | }
447 |
448 | // https://github.com/wechat-miniprogram/api-typings/issues/332
449 | {
450 | Component({
451 | data: {
452 | a: ''
453 | },
454 | methods: {
455 | test() {
456 | expectError(this.data.xxx)
457 | },
458 | }
459 | })
460 | }
461 |
462 | // https://github.com/wechat-miniprogram/api-typings/issues/332
463 | {
464 | interface Foo {
465 | f: string
466 | }
467 |
468 | Component({
469 | properties: {
470 | bar: {
471 | type: Object,
472 | value: { f: '' } as Foo,
473 | observer () {}
474 | },
475 | foo: {
476 | type: Array,
477 | value: [] as Foo[],
478 | }
479 | },
480 | methods: {
481 | getData() {
482 | return this.data.foo
483 | },
484 | test() {
485 | expectType(this.data.bar)
486 | expectType(this.properties.bar)
487 | expectType(this.getData())
488 | },
489 | }
490 | })
491 | }
492 |
493 | // https://github.com/wechat-miniprogram/api-typings/issues/332
494 | {
495 | const bA = Behavior({
496 | properties: {
497 | pA: {
498 | type: String,
499 | value: '',
500 | },
501 | pA1: Boolean
502 | },
503 | data: {
504 | dataA: 'init data',
505 | },
506 | methods: {
507 | methodA() {
508 | return this.data.dataA
509 | },
510 | },
511 | })
512 | const bB = Behavior({
513 | properties: {
514 | pB: {
515 | type: Array,
516 | value: [] as string[],
517 | }
518 | },
519 | data: {
520 | dataB: [] as string[],
521 | },
522 | methods: {
523 | methodB() {
524 | return this.data.dataB
525 | },
526 | },
527 | })
528 |
529 | Component({
530 | behaviors: [bA, bB],
531 | methods: {
532 | test() {
533 | expectType(this.data.pA)
534 | expectType(this.data.pA1)
535 | expectType(this.data.dataA)
536 | expectType(this.data.pB)
537 | expectType(this.data.dataB)
538 | expectType(this.methodA())
539 | expectType(this.methodB())
540 | },
541 | }
542 | })
543 | }
544 | // https://github.com/wechat-miniprogram/api-typings/issues/332#issuecomment-2333434425
545 | {
546 | const b1 = Behavior({
547 | properties: {
548 | pA: {
549 | type: String,
550 | value: '',
551 | },
552 | pA1: Boolean
553 | },
554 | data: {
555 | dataA: 'init data',
556 | },
557 | methods: {
558 | methodA() {
559 | return this.data.dataA
560 | },
561 | },
562 | })
563 | const b2 = Behavior({
564 | behaviors: [b1],
565 | properties: {
566 | pB: {
567 | type: Array,
568 | value: [] as string[],
569 | }
570 | },
571 | data: {
572 | dataB: [] as string[],
573 | },
574 | methods: {
575 | methodB() {
576 | return this.data.dataB
577 | },
578 | test() {
579 | expectType(this.data.pA)
580 | expectType(this.data.pA1)
581 | expectType(this.data.dataA)
582 | expectType(this.methodA())
583 | },
584 | },
585 | })
586 |
587 | Component({
588 | behaviors: [b2],
589 | methods: {
590 | test() {
591 | expectType(this.data.pA)
592 | expectType(this.data.pA1)
593 | expectType(this.data.dataA)
594 | expectType(this.data.pB)
595 | expectType(this.data.dataB)
596 | expectType(this.methodA())
597 | expectType(this.methodB())
598 | },
599 | }
600 | })
601 | }
602 |
603 | {
604 | const b1 = Behavior({
605 | methods: {
606 | methodA() {
607 | return ['']
608 | },
609 | },
610 | })
611 | const b2 = Behavior({
612 | properties: {
613 | pB: {
614 | type: Array,
615 | value: [] as string[],
616 | }
617 | }
618 | })
619 | const b3 = Behavior({
620 | data: {
621 | dataC: 'init data',
622 | }
623 | })
624 |
625 | Component({
626 | behaviors: [b1, b2, b3],
627 | methods: {
628 | test() {
629 | expectType(this.data.pB)
630 | expectType(this.data.dataC)
631 | expectType(this.methodA())
632 | expectError(this.data.xxx)
633 | expectError(this.properties.yyy)
634 | },
635 | }
636 | })
637 | }
--------------------------------------------------------------------------------
/test/page.test.ts:
--------------------------------------------------------------------------------
1 | import { expectType, expectError } from 'tsd'
2 |
3 | expectType(Page({}))
4 |
5 | expectType>(getCurrentPages()[0].data)
6 |
7 | const app = getApp<{
8 | globalData: {
9 | userInfo: WechatMiniprogram.UserInfo
10 | }
11 | userInfoReadyCallback(userInfo: WechatMiniprogram.UserInfo): void
12 | }>()
13 |
14 | Page({
15 | data: {
16 | motto: '点击 “编译” 以构建',
17 | userInfo: {},
18 | hasUserInfo: false,
19 | canIUse: wx.canIUse('button.open-type.getUserInfo'),
20 | },
21 | bindViewTap() {
22 | wx.navigateTo({
23 | url: '../logs/logs',
24 | })
25 | },
26 | onLoad() {
27 | if (app.globalData.userInfo) {
28 | this.setData({
29 | userInfo: app.globalData.userInfo,
30 | hasUserInfo: true,
31 | })
32 | } else if (this.data.canIUse) {
33 | app.userInfoReadyCallback = res => {
34 | this.setData({
35 | userInfo: res,
36 | hasUserInfo: true,
37 | })
38 | }
39 | } else {
40 | wx.getUserInfo({
41 | success: res => {
42 | app.globalData.userInfo = res.userInfo
43 | this.setData({
44 | userInfo: res.userInfo,
45 | hasUserInfo: true,
46 | })
47 | },
48 | })
49 | }
50 | },
51 |
52 | getUserInfo(e: any) {
53 | this.selectComponent('test')
54 | app.globalData.userInfo = e.detail.userInfo
55 | this.setData({
56 | userInfo: e.detail.userInfo,
57 | hasUserInfo: true,
58 | })
59 | },
60 | })
61 |
62 | Page({
63 | data: {
64 | text: 'init data',
65 | array: [{ msg: '1' }, { msg: '2' }],
66 | logs: [] as string[],
67 | },
68 | onLoad(options) {
69 | expectType(options.from)
70 | const app = getApp<{
71 | globalData: { userInfo: WechatMiniprogram.UserInfo }
72 | }>()
73 | expectType(app.globalData.userInfo.nickName)
74 | },
75 | onReady() {
76 | this.setData({
77 | logs: (wx.getStorageSync('logs') || []).map((log: number) => {
78 | return new Date(log).toString()
79 | }),
80 | })
81 | },
82 | onShow() {},
83 | onUnload() {},
84 | onPullDownRefresh() {},
85 | onShareAppMessage(res) {
86 | expectType<'button' | 'menu'>(res.from)
87 | if (res.from === 'button') {
88 | expectType(res.webViewUrl)
89 | }
90 | return {
91 | title: '自定义转发标题',
92 | path: '/page/user?id=123',
93 | }
94 | },
95 | onPageScroll() {},
96 | onResize() {},
97 | onTabItemTap(item) {
98 | expectType(item.index)
99 | expectType(item.pagePath)
100 | expectType(item.text)
101 | },
102 | viewTap() {
103 | this.setData(
104 | {
105 | text: 'Set some data for updating view.',
106 | 'array[0].text': 'changed data',
107 | 'object.text': 'changed data',
108 | 'newField.text': 'new data',
109 | },
110 | function() {},
111 | )
112 | expectType(this.route)
113 | expectType(this.data.text)
114 | this.viewTap()
115 |
116 | const p = getCurrentPages()[1] as WechatMiniprogram.Page.Instance<
117 | { a: number },
118 | { customData: { b: number } }
119 | >
120 | p.customData.b = p.data.a
121 | },
122 | customData: {
123 | hi: 'MINA',
124 | },
125 | })
126 |
127 | Page({
128 | data: {
129 | a: 1,
130 | },
131 | onLoad(q) {
132 | expectType>(q)
133 | expectType(this.data.a)
134 | expectError(this.a)
135 | },
136 | jump() {
137 | const query = wx.createSelectorQuery()
138 | query.select('#a').boundingClientRect(res => {
139 | expectType(res)
140 | })
141 | query.selectViewport().scrollOffset(res => {
142 | expectType(res)
143 | })
144 | query.exec(res => {
145 | expectType(res)
146 | })
147 | },
148 | jumpBack() {
149 | wx.navigateBack({})
150 | },
151 | })
152 |
153 | Page({
154 | f() {
155 | expectType>(this.data)
156 | },
157 | })
158 |
159 | Page({
160 | data: {},
161 | f() {
162 | expectType<{}>(this.data)
163 | this.setData({
164 | a: 1,
165 | })
166 | },
167 | })
168 |
169 | Page({
170 | onLoad(q) {
171 | q
172 | },
173 | f() {
174 | this.onLoad()
175 | },
176 | })
177 |
178 | interface DataType {
179 | logs: string[]
180 | }
181 | interface CustomOption {
182 | getLogs(): string[]
183 | }
184 |
185 | Page({
186 | data: {
187 | logs: [],
188 | },
189 | getLogs() {
190 | return (wx.getStorageSync('logs') || []).map((log: number) => {
191 | return new Date(log).toString()
192 | })
193 | },
194 | onLoad() {
195 | const logs = this.getLogs()
196 | expectType(logs)
197 | this.setData({ logs })
198 | expectError(this.logs)
199 | expectType(this.data.logs)
200 | },
201 | })
202 |
203 | Page({
204 | test() {
205 | const channel = this.getOpenerEventChannel()
206 | expectType(channel)
207 | channel.emit('test', {})
208 | channel.on('xxx', () => {})
209 | expectError(channel.emit(1, 2))
210 | },
211 | })
212 |
213 | Page({
214 | onAddToFavorites(res) {
215 | // webview 页面返回 webviewUrl
216 | if (res.webviewUrl) {
217 | console.log('WebviewUrl: ', res.webviewUrl)
218 | }
219 | return {
220 | title: '自定义标题',
221 | imageUrl: 'http://demo.png',
222 | query: 'name=xxx&age=xxx',
223 | }
224 | },
225 | })
226 |
227 | Page({
228 | data: { a: '123' },
229 | onShow() {
230 | expectType<() => number>(this.fn)
231 | },
232 | fn() {
233 | const a: number = 1
234 | return a
235 | },
236 | onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent {
237 | return { title: this.data.a, imageUrl: '', path: '' }
238 | },
239 | })
--------------------------------------------------------------------------------
/test/xr-frame.test.ts:
--------------------------------------------------------------------------------
1 | import { expectType } from 'tsd'
2 |
3 | import type * as XrFrame from 'XrFrame'
4 |
5 | const xrFrameSystem = wx.getXrFrameSystem()
6 |
7 | expectType