├── .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 | [![Published on DefinitelyTyped](https://img.shields.io/npm/v/@types/wechat-miniprogram?label=%40types)](https://www.npmjs.com/package/@types/wechat-miniprogram) 6 | [![Published on NPM](https://img.shields.io/npm/v/miniprogram-api-typings.svg?style=flat)](https://www.npmjs.com/package/miniprogram-api-typings) 7 | [![MIT License](https://img.shields.io/github/license/wechat-miniprogram/api-typings.svg)](https://github.com/wechat-miniprogram/api-typings) 8 | [![GitHub Actions Test Status](https://github.com/wechat-miniprogram/api-typings/actions/workflows/test.yml/badge.svg?branch=master)](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 | [![已在 DefinitelyTyped 发布](https://img.shields.io/npm/v/@types/wechat-miniprogram?label=%40types)](https://www.npmjs.com/package/@types/wechat-miniprogram) 6 | [![已在 NPM 发布](https://img.shields.io/npm/v/miniprogram-api-typings.svg?style=flat)](https://www.npmjs.com/package/miniprogram-api-typings) 7 | [![MIT 协议](https://img.shields.io/github/license/wechat-miniprogram/api-typings.svg)](https://github.com/wechat-miniprogram/api-typings) 8 | [![GitHub Actions 测试状况](https://github.com/wechat-miniprogram/api-typings/actions/workflows/test.yml/badge.svg?branch=master)](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(new xrFrameSystem.Element('test', () => {})) 8 | 9 | const AutoRotateTouchableGLTFDefaultComponents: XrFrame.IEntityComponents = Object.assign({ 10 | 'mesh-shape': {}, 11 | 'auto-rotate': {} 12 | }, xrFrameSystem.GLTFDefaultComponents) 13 | 14 | const AutoRotateTouchableGLTFDataMapping: {[key: string]: string[]} = Object.assign({ 15 | speed: ['auto-rotate', 'speed'] 16 | }, xrFrameSystem.GLTFDataMapping) 17 | 18 | const Element = xrFrameSystem.Element 19 | 20 | class XRAutoRotateTouchableGLTF extends Element { 21 | public readonly defaultComponents: XrFrame.IEntityComponents = AutoRotateTouchableGLTFDefaultComponents 22 | public readonly dataMapping: {[key: string]: string[]} = AutoRotateTouchableGLTFDataMapping 23 | } 24 | 25 | xrFrameSystem.registerElement('auto-rotate-touchable-gltf', XRAutoRotateTouchableGLTF) 26 | 27 | xrFrameSystem.registerMaterial('shining', scene => { 28 | return scene.createMaterial(scene.assets.getAsset('effect', 'shining')) 29 | }) 30 | 31 | const ShiningStarDefaultComponents: XrFrame.IEntityComponents = Object.assign({ 32 | mesh: { 33 | geometry: 'star', 34 | material: 'shining' 35 | } 36 | }, xrFrameSystem.NodeDefaultComponents) 37 | 38 | const ShiningStarDataMapping: {[key: string]: string[]} = Object.assign({ 39 | uniforms: ['mesh', 'uniforms'] 40 | }, xrFrameSystem.NodeDataMapping) 41 | 42 | xrFrameSystem.registerElement('shining-star', class XRShiningStar extends Element { 43 | public readonly defaultComponents: XrFrame.IEntityComponents = ShiningStarDefaultComponents 44 | public readonly dataMapping: {[key: string]: string[]} = ShiningStarDataMapping 45 | }) 46 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowJs": false, 4 | "alwaysStrict": true, 5 | "experimentalDecorators": true, 6 | "inlineSourceMap": true, 7 | "inlineSources": true, 8 | "lib": ["es6"], 9 | "module": "CommonJS", 10 | "noFallthroughCasesInSwitch": true, 11 | "noImplicitAny": true, 12 | "noImplicitReturns": true, 13 | "noImplicitThis": true, 14 | "noUnusedLocals": true, 15 | "noUnusedParameters": true, 16 | "pretty": true, 17 | "removeComments": true, 18 | "strict": true, 19 | "strictNullChecks": true, 20 | "strictPropertyInitialization": true, 21 | "target": "ES5", 22 | "typeRoots": [ "./node_modules/**/*.d.ts" ] 23 | }, 24 | "include": [ 25 | "./test/*.ts" 26 | ], 27 | "exclude": [ 28 | "node_modules" 29 | ] 30 | } 31 | -------------------------------------------------------------------------------- /types/index.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /types/wx/index.d.ts: -------------------------------------------------------------------------------- 1 | /*! ***************************************************************************** 2 | Copyright (c) 2025 Tencent, Inc. All rights reserved. 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | this software and associated documentation files (the "Software"), to deal in 6 | the Software without restriction, including without limitation the rights to 7 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 8 | of the Software, and to permit persons to whom the Software is furnished to do 9 | so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | SOFTWARE. 21 | ***************************************************************************** */ 22 | 23 | /// 24 | /// 25 | /// 26 | /// 27 | /// 28 | /// 29 | /// 30 | /// 31 | /// 32 | 33 | declare namespace WechatMiniprogram { 34 | type IAnyObject = Record 35 | type Optional = F extends (arg: infer P) => infer R ? (arg?: P) => R : F 36 | type OptionalInterface = { [K in keyof T]: Optional } 37 | interface AsyncMethodOptionLike { 38 | success?: (...args: any[]) => void 39 | } 40 | type PromisifySuccessResult< 41 | P, 42 | T extends AsyncMethodOptionLike 43 | > = P extends { 44 | success: any 45 | } 46 | ? void 47 | : P extends { fail: any } 48 | ? void 49 | : P extends { complete: any } 50 | ? void 51 | : Promise>[0]> 52 | 53 | // TODO: Extract real definition from `lib.dom.d.ts` to replace this 54 | type IIRFilterNode = any 55 | type WaveShaperNode = any 56 | type ConstantSourceNode = any 57 | type OscillatorNode = any 58 | type GainNode = any 59 | type BiquadFilterNode = any 60 | type PeriodicWaveNode = any 61 | type AudioNode = any 62 | type ChannelSplitterNode = any 63 | type ChannelMergerNode = any 64 | type DelayNode = any 65 | type DynamicsCompressorNode = any 66 | type ScriptProcessorNode = any 67 | type PannerNode = any 68 | type AnalyserNode = any 69 | type WebGLTexture = any 70 | type WebGLRenderingContext = any 71 | 72 | // TODO: fill worklet type 73 | type WorkletFunction = (...args: any) => any 74 | type AnimationObject = any 75 | type SharedValue = T 76 | type DerivedValue = T 77 | } 78 | 79 | declare let console: WechatMiniprogram.Console 80 | 81 | declare let wx: WechatMiniprogram.Wx 82 | /** 引入模块。返回模块通过 `module.exports` 或 `exports` 暴露的接口。 */ 83 | interface Require { 84 | ( 85 | /** 需要引入模块文件相对于当前文件的相对路径,或 npm 模块名,或 npm 模块路径。不支持绝对路径 */ 86 | module: string, 87 | /** 用于异步获取其他分包中的模块的引用结果,详见 [分包异步化]((subpackages/async)) */ 88 | callback?: (moduleExport: any) => void, 89 | /** 异步获取分包失败时的回调,详见 [分包异步化]((subpackages/async)) */ 90 | errorCallback?: (err: any) => void 91 | ): any 92 | /** 以 Promise 形式异步引入模块。返回模块通过 `module.exports` 或 `exports` 暴露的接口。 */ 93 | async( 94 | /** 需要引入模块文件相对于当前文件的相对路径,或 npm 模块名,或 npm 模块路径。不支持绝对路径 */ 95 | module: string 96 | ): Promise 97 | } 98 | declare const require: Require 99 | /** 引入插件。返回插件通过 `main` 暴露的接口。 */ 100 | interface RequirePlugin { 101 | ( 102 | /** 需要引入的插件的 alias */ 103 | module: string, 104 | /** 用于异步获取其他分包中的插件的引用结果,详见 [分包异步化]((subpackages/async)) */ 105 | callback?: (pluginExport: any) => void 106 | ): any 107 | /** 以 Promise 形式异步引入插件。返回插件通过 `main` 暴露的接口。 */ 108 | async( 109 | /** 需要引入的插件的 alias */ 110 | module: string 111 | ): Promise 112 | } 113 | declare const requirePlugin: RequirePlugin 114 | /** 插件引入当前使用者小程序。返回使用者小程序通过 [插件配置中 `export` 暴露的接口](https://developers.weixin.qq.com/miniprogram/dev/framework/plugin/using.html#%E5%AF%BC%E5%87%BA%E5%88%B0%E6%8F%92%E4%BB%B6)。 115 | * 116 | * 该接口只在插件中存在 117 | * 118 | * 最低基础库: `2.11.1` */ 119 | declare function requireMiniProgram(): any 120 | /** 当前模块对象 */ 121 | declare let module: { 122 | /** 模块向外暴露的对象,使用 `require` 引用该模块时可以获取 */ 123 | exports: any 124 | } 125 | /** `module.exports` 的引用 */ 126 | declare let exports: any 127 | 128 | /** [clearInterval(number intervalID)](https://developers.weixin.qq.com/miniprogram/dev/api/base/timer/clearInterval.html) 129 | * 130 | * 取消由 setInterval 设置的定时器。 */ 131 | declare function clearInterval( 132 | /** 要取消的定时器的 ID */ 133 | intervalID: number 134 | ): void 135 | /** [clearTimeout(number timeoutID)](https://developers.weixin.qq.com/miniprogram/dev/api/base/timer/clearTimeout.html) 136 | * 137 | * 取消由 setTimeout 设置的定时器。 */ 138 | declare function clearTimeout( 139 | /** 要取消的定时器的 ID */ 140 | timeoutID: number 141 | ): void 142 | /** [number setInterval(function callback, number delay, any rest)](https://developers.weixin.qq.com/miniprogram/dev/api/base/timer/setInterval.html) 143 | * 144 | * 设定一个定时器。按照指定的周期(以毫秒计)来执行注册的回调函数 */ 145 | declare function setInterval( 146 | /** 回调函数 */ 147 | callback: (...args: any[]) => any, 148 | /** 执行回调函数之间的时间间隔,单位 ms。 */ 149 | delay?: number, 150 | /** param1, param2, ..., paramN 等附加参数,它们会作为参数传递给回调函数。 */ 151 | ...rest: any[] 152 | ): number 153 | /** [number setTimeout(function callback, number delay, any rest)](https://developers.weixin.qq.com/miniprogram/dev/api/base/timer/setTimeout.html) 154 | * 155 | * 设定一个定时器。在定时到期以后执行注册的回调函数 */ 156 | declare function setTimeout( 157 | /** 回调函数 */ 158 | callback: (...args: any[]) => any, 159 | /** 延迟的时间,函数的调用会在该延迟之后发生,单位 ms。 */ 160 | delay?: number, 161 | /** param1, param2, ..., paramN 等附加参数,它们会作为参数传递给回调函数。 */ 162 | ...rest: any[] 163 | ): number 164 | -------------------------------------------------------------------------------- /types/wx/lib.wx.app.d.ts: -------------------------------------------------------------------------------- 1 | /*! ***************************************************************************** 2 | Copyright (c) 2025 Tencent, Inc. All rights reserved. 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | this software and associated documentation files (the "Software"), to deal in 6 | the Software without restriction, including without limitation the rights to 7 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 8 | of the Software, and to permit persons to whom the Software is furnished to do 9 | so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | SOFTWARE. 21 | ***************************************************************************** */ 22 | 23 | declare namespace WechatMiniprogram.App { 24 | type SceneValues = 25 | | 1000 26 | | 1001 27 | | 1005 28 | | 1006 29 | | 1007 30 | | 1008 31 | | 1010 32 | | 1011 33 | | 1012 34 | | 1013 35 | | 1014 36 | | 1017 37 | | 1019 38 | | 1020 39 | | 1022 40 | | 1023 41 | | 1024 42 | | 1025 43 | | 1026 44 | | 1027 45 | | 1028 46 | | 1029 47 | | 1030 48 | | 1031 49 | | 1032 50 | | 1034 51 | | 1035 52 | | 1036 53 | | 1037 54 | | 1038 55 | | 1039 56 | | 1042 57 | | 1043 58 | | 1044 59 | | 1045 60 | | 1046 61 | | 1047 62 | | 1048 63 | | 1049 64 | | 1052 65 | | 1053 66 | | 1054 67 | | 1056 68 | | 1057 69 | | 1058 70 | | 1059 71 | | 1060 72 | | 1064 73 | | 1065 74 | | 1067 75 | | 1068 76 | | 1069 77 | | 1071 78 | | 1072 79 | | 1073 80 | | 1074 81 | | 1077 82 | | 1078 83 | | 1079 84 | | 1081 85 | | 1082 86 | | 1084 87 | | 1088 88 | | 1089 89 | | 1090 90 | | 1091 91 | | 1092 92 | | 1095 93 | | 1096 94 | | 1097 95 | | 1099 96 | | 1100 97 | | 1101 98 | | 1102 99 | | 1103 100 | | 1104 101 | | 1106 102 | | 1107 103 | | 1113 104 | | 1114 105 | | 1119 106 | | 1120 107 | | 1121 108 | | 1124 109 | | 1125 110 | | 1126 111 | | 1129 112 | | 1131 113 | | 1133 114 | | 1135 115 | | 1144 116 | | 1145 117 | | 1146 118 | | 1148 119 | | 1150 120 | | 1151 121 | | 1152 122 | | 1153 123 | | 1154 124 | | 1155 125 | | 1157 126 | | 1158 127 | | 1160 128 | | 1167 129 | | 1168 130 | | 1169 131 | | 1171 132 | | 1173 133 | | 1175 134 | | 1176 135 | | 1177 136 | | 1178 137 | | 1179 138 | | 1181 139 | | 1183 140 | | 1184 141 | | 1185 142 | | 1186 143 | | 1187 144 | | 1189 145 | | 1191 146 | | 1192 147 | | 1193 148 | | 1194 149 | | 1195 150 | | 1196 151 | | 1197 152 | | 1198 153 | | 1200 154 | | 1201 155 | | 1202 156 | | 1203 157 | | 1206 158 | | 1207 159 | | 1208 160 | | 1212 161 | | 1215 162 | | 1216 163 | | 1223 164 | | 1228 165 | | 1231 166 | 167 | interface LaunchShowOption { 168 | /** 打开小程序的路径 */ 169 | path: string 170 | /** 打开小程序的query */ 171 | query: IAnyObject 172 | /** 打开小程序的场景值 173 | * - 1000:其他 174 | * - 1001:发现栏小程序主入口,「最近使用」列表(基础库 2.2.4 版本起包含「我的小程序」列表) 175 | * - 1005:微信首页顶部搜索框的搜索结果页 176 | * - 1006:发现栏小程序主入口搜索框的搜索结果页 177 | * - 1007:单人聊天会话中的小程序消息卡片 178 | * - 1008:群聊会话中的小程序消息卡片 179 | * - 1010:收藏夹 180 | * - 1011:扫描二维码 181 | * - 1012:长按图片识别二维码 182 | * - 1013:扫描手机相册中选取的二维码 183 | * - 1014:小程序订阅消息(与 1107 相同) 184 | * - 1017:前往小程序体验版的入口页 185 | * - 1019:微信钱包(微信客户端 7.0.0 版本改为支付入口) 186 | * - 1020:公众号 profile 页相关小程序列表(已废弃) 187 | * - 1022:聊天顶部置顶小程序入口(微信客户端 6.6.1 版本起废弃) 188 | * - 1023:安卓系统桌面图标 189 | * - 1024:小程序 profile 页 190 | * - 1025:扫描一维码 191 | * - 1026:发现栏小程序主入口,「附近的小程序」列表 192 | * - 1027:微信首页顶部搜索框搜索结果页「使用过的小程序」列表 193 | * - 1028:我的卡包 194 | * - 1029:小程序中的卡券详情页 195 | * - 1030:自动化测试下打开小程序 196 | * - 1031:长按图片识别一维码 197 | * - 1032:扫描手机相册中选取的一维码 198 | * - 1034:微信支付完成页 199 | * - 1035:公众号自定义菜单 200 | * - 1036:App 分享消息卡片 201 | * - 1037:小程序打开小程序 202 | * - 1038:从另一个小程序返回 203 | * - 1039:摇电视 204 | * - 1042:添加好友搜索框的搜索结果页 205 | * - 1043:公众号模板消息 206 | * - 1044:带 shareTicket 的小程序消息卡片 [详情](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share.html) 207 | * - 1045:朋友圈广告 208 | * - 1046:朋友圈广告详情页 209 | * - 1047:扫描小程序码 210 | * - 1048:长按图片识别小程序码 211 | * - 1049:扫描手机相册中选取的小程序码 212 | * - 1052:卡券的适用门店列表 213 | * - 1053:搜一搜的结果页 214 | * - 1054:顶部搜索框小程序快捷入口(微信客户端版本 6.7.4 起废弃) 215 | * - 1056:聊天顶部音乐播放器右上角菜单 216 | * - 1057:钱包中的银行卡详情页 217 | * - 1058:公众号文章 218 | * - 1059:体验版小程序绑定邀请页 219 | * - 1060:微信支付完成页(与 1034 相同) 220 | * - 1064:微信首页连 Wi-Fi 状态栏 221 | * - 1065:URL scheme [详情](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/url-scheme.html) 222 | * - 1067:公众号文章广告 223 | * - 1068:附近小程序列表广告(已废弃) 224 | * - 1069:移动应用通过 openSDK 进入微信,打开小程序 225 | * - 1071:钱包中的银行卡列表页 226 | * - 1072:二维码收款页面 227 | * - 1073:客服消息列表下发的小程序消息卡片 228 | * - 1074:公众号会话下发的小程序消息卡片 229 | * - 1077:摇周边 230 | * - 1078:微信连 Wi-Fi 成功提示页 231 | * - 1079:微信游戏中心 232 | * - 1081:客服消息下发的文字链 233 | * - 1082:公众号会话下发的文字链 234 | * - 1084:朋友圈广告原生页 235 | * - 1088:会话中查看系统消息,打开小程序 236 | * - 1089:微信聊天主界面下拉,「最近使用」栏(基础库 2.2.4 版本起包含「我的小程序」栏) 237 | * - 1090:长按小程序右上角菜单唤出最近使用历史 238 | * - 1091:公众号文章商品卡片 239 | * - 1092:城市服务入口 240 | * - 1095:小程序广告组件 241 | * - 1096:聊天记录,打开小程序 242 | * - 1097:微信支付签约原生页,打开小程序 243 | * - 1099:页面内嵌插件 244 | * - 1100:红包封面详情页打开小程序 245 | * - 1101:远程调试热更新(开发者工具中,预览 -> 自动预览 -> 编译并预览) 246 | * - 1102:公众号 profile 页服务预览 247 | * - 1103:发现栏小程序主入口,「我的小程序」列表(基础库 2.2.4 版本起废弃) 248 | * - 1104:微信聊天主界面下拉,「我的小程序」栏(基础库 2.2.4 版本起废弃) 249 | * - 1106:聊天主界面下拉,从顶部搜索结果页,打开小程序 250 | * - 1107:订阅消息,打开小程序 251 | * - 1113:安卓手机负一屏,打开小程序(三星) 252 | * - 1114:安卓手机侧边栏,打开小程序(三星) 253 | * - 1119:【企业微信】工作台内打开小程序 254 | * - 1120:【企业微信】个人资料页内打开小程序 255 | * - 1121:【企业微信】聊天加号附件框内打开小程序 256 | * - 1124:扫“一物一码”打开小程序 257 | * - 1125:长按图片识别“一物一码” 258 | * - 1126:扫描手机相册中选取的“一物一码” 259 | * - 1129:微信爬虫访问 [详情](https://developers.weixin.qq.com/miniprogram/dev/reference/configuration/sitemap.html) 260 | * - 1131:浮窗(8.0 版本起仅包含被动浮窗) 261 | * - 1133:硬件设备打开小程序 [详情](https://developers.weixin.qq.com/doc/oplatform/Miniprogram_Frame/) 262 | * - 1135:小程序 profile 页相关小程序列表,打开小程序 263 | * - 1144:公众号文章 - 视频贴片 264 | * - 1145:发现栏 - 发现小程序 265 | * - 1146:地理位置信息打开出行类小程序 266 | * - 1148:卡包-交通卡,打开小程序 267 | * - 1150:扫一扫商品条码结果页打开小程序 268 | * - 1151:发现栏 - 我的订单 269 | * - 1152:订阅号视频打开小程序 270 | * - 1153:“识物”结果页打开小程序 271 | * - 1154:朋友圈内打开“单页模式” 272 | * - 1155:“单页模式”打开小程序 273 | * - 1157:服务号会话页打开小程序 274 | * - 1158:群工具打开小程序 275 | * - 1160:群待办 276 | * - 1167:H5 通过开放标签打开小程序 [详情](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html) 277 | * - 1168:移动应用直接运行小程序 278 | * - 1169:发现栏小程序主入口,各个生活服务入口(例如快递服务、出行服务等) 279 | * - 1171:微信运动记录(仅安卓) 280 | * - 1173:聊天素材用小程序打开 [详情](https://developers.weixin.qq.com/miniprogram/dev/framework/material/support_material.html) 281 | * - 1175:视频号主页商店入口 282 | * - 1176:视频号直播间主播打开小程序 283 | * - 1177:视频号直播商品 284 | * - 1178:在电脑打开手机上打开的小程序 285 | * - 1179:#话题页打开小程序 286 | * - 1181:网站应用打开 PC 小程序 287 | * - 1183:PC 微信 - 小程序面板 - 发现小程序 - 搜索 288 | * - 1184:视频号链接打开小程序 289 | * - 1185:群公告 290 | * - 1186:收藏 - 笔记 291 | * - 1187:浮窗(8.0 版本起) 292 | * - 1189:表情雨广告 293 | * - 1191:视频号活动 294 | * - 1192:企业微信联系人 profile 页 295 | * - 1193:视频号主页服务菜单打开小程序 296 | * - 1194:URL Link [详情](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/url-link.html) 297 | * - 1195:视频号主页商品 tab 298 | * - 1196:个人状态打开小程序 299 | * - 1197:视频号主播从直播间返回小游戏 300 | * - 1198:视频号开播界面打开小游戏 301 | * - 1200:视频号广告打开小程序 302 | * - 1201:视频号广告详情页打开小程序 303 | * - 1202:企微客服号会话打开小程序卡片 304 | * - 1203:微信小程序压测工具的请求 305 | * - 1206:视频号小游戏直播间打开小游戏 306 | * - 1207:企微客服号会话打开小程序文字链 307 | * - 1208:聊天打开商品卡片 308 | * - 1212:青少年模式申请页打开小程序 309 | * - 1215:广告预约打开小程序 310 | * - 1216:视频号订单中心打开小程序 311 | * - 1223:安卓桌面 Widget 打开小程序 312 | * - 1228:视频号原生广告组件打开小程序 313 | * - 1231:动态消息提醒入口打开小程序 314 | */ 315 | scene: SceneValues 316 | /** shareTicket,详见 [获取更多转发信息]((转发#获取更多转发信息)) */ 317 | shareTicket: string 318 | /** 当场景为由从另一个小程序或公众号或App打开时,返回此字段 */ 319 | referrerInfo?: ReferrerInfo 320 | /** 打开的文件信息数组,只有从聊天素材场景打开(scene为1173)才会携带该参数 */ 321 | forwardMaterials: ForwardMaterials[] 322 | /** 从微信群聊/单聊打开小程序时,chatType 表示具体微信群聊/单聊类型 323 | * 324 | * 可选值: 325 | * - 1: 微信联系人单聊; 326 | * - 2: 企业微信联系人单聊; 327 | * - 3: 普通微信群聊; 328 | * - 4: 企业微信互通群聊; */ 329 | chatType?: 1 | 2 | 3 | 4 330 | /** 需要基础库: `2.20.0` 331 | * 332 | * API 类别 333 | * 334 | * 可选值: 335 | * - 'default': 默认类别; 336 | * - 'nativeFunctionalized': 原生功能化,视频号直播商品、商品橱窗等场景打开的小程序; 337 | * - 'browseOnly': 仅浏览,朋友圈快照页等场景打开的小程序; 338 | * - 'embedded': 内嵌,通过打开半屏小程序能力打开的小程序; */ 339 | apiCategory: 340 | | 'default' 341 | | 'nativeFunctionalized' 342 | | 'browseOnly' 343 | | 'embedded' 344 | } 345 | 346 | interface PageNotFoundOption { 347 | /** 不存在页面的路径 */ 348 | path: string 349 | /** 打开不存在页面的 query */ 350 | query: IAnyObject 351 | /** 是否本次启动的首个页面(例如从分享等入口进来,首个页面是开发者配置的分享页面) */ 352 | isEntryPage: boolean 353 | } 354 | 355 | interface Option { 356 | /** 生命周期回调—监听小程序初始化 357 | * 358 | * 小程序初始化完成时触发,全局只触发一次。 359 | */ 360 | onLaunch(options: LaunchShowOption): void 361 | /** 生命周期回调—监听小程序显示 362 | * 363 | * 小程序启动,或从后台进入前台显示时 364 | */ 365 | onShow(options: LaunchShowOption): void 366 | /** 生命周期回调—监听小程序隐藏 367 | * 368 | * 小程序从前台进入后台时 369 | */ 370 | onHide(): void 371 | /** 错误监听函数 372 | * 373 | * 小程序发生脚本错误,或者 api 374 | */ 375 | onError(/** 错误信息,包含堆栈 */ error: string): void 376 | /** 页面不存在监听函数 377 | * 378 | * 小程序要打开的页面不存在时触发,会带上页面信息回调该函数 379 | * 380 | * **注意:** 381 | * 1. 如果开发者没有添加 `onPageNotFound` 监听,当跳转页面不存在时,将推入微信客户端原生的页面不存在提示页面。 382 | * 2. 如果 `onPageNotFound` 回调中又重定向到另一个不存在的页面,将推入微信客户端原生的页面不存在提示页面,并且不再回调 `onPageNotFound`。 383 | * 384 | * 最低基础库: 1.9.90 385 | */ 386 | onPageNotFound(options: PageNotFoundOption): void 387 | /** 388 | * 小程序有未处理的 Promise 拒绝时触发。也可以使用 [wx.onUnhandledRejection](https://developers.weixin.qq.com/miniprogram/dev/api/base/app/app-event/wx.onUnhandledRejection.html) 绑定监听。注意事项请参考 [wx.onUnhandledRejection](https://developers.weixin.qq.com/miniprogram/dev/api/base/app/app-event/wx.onUnhandledRejection.html)。 389 | * **参数**:与 [wx.onUnhandledRejection](https://developers.weixin.qq.com/miniprogram/dev/api/base/app/app-event/wx.onUnhandledRejection.html) 一致 390 | */ 391 | onUnhandledRejection: OnUnhandledRejectionCallback 392 | /** 393 | * 系统切换主题时触发。也可以使用 wx.onThemeChange 绑定监听。 394 | * 395 | * 最低基础库: 2.11.0 396 | */ 397 | onThemeChange: OnThemeChangeCallback 398 | } 399 | 400 | type Instance = Option & T 401 | type Options = Partial

= Component.Property

77 | type Method = Component.Method 78 | type Behavior = Component.Behavior 79 | 80 | type DefinitionFilter = Component.DefinitionFilter 81 | type Lifetimes = Component.Lifetimes 82 | type OtherOption = Omit 83 | } 84 | /** 注册一个 `behavior`,接受一个 `Object` 类型的参数。*/ 85 | declare let Behavior: WechatMiniprogram.Behavior.Constructor 86 | -------------------------------------------------------------------------------- /types/wx/lib.wx.cloud.d.ts: -------------------------------------------------------------------------------- 1 | /*! ***************************************************************************** 2 | Copyright (c) 2025 Tencent, Inc. All rights reserved. 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | this software and associated documentation files (the "Software"), to deal in 6 | the Software without restriction, including without limitation the rights to 7 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 8 | of the Software, and to permit persons to whom the Software is furnished to do 9 | so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | SOFTWARE. 21 | ***************************************************************************** */ 22 | 23 | /// 24 | 25 | /** 26 | * Common interfaces and types 27 | */ 28 | 29 | interface IAPIError { 30 | errMsg: string 31 | } 32 | 33 | interface IAPIParam { 34 | config?: ICloudConfig 35 | success?: (res: T) => void 36 | fail?: (err: IAPIError) => void 37 | complete?: (val: T | IAPIError) => void 38 | } 39 | 40 | interface IAPISuccessParam { 41 | errMsg: string 42 | } 43 | 44 | type IAPICompleteParam = IAPISuccessParam | IAPIError 45 | 46 | type IAPIFunction> = (param?: P) => Promise 47 | 48 | interface IInitCloudConfig { 49 | env?: 50 | | string 51 | | { 52 | database?: string 53 | functions?: string 54 | storage?: string 55 | } 56 | traceUser?: boolean 57 | } 58 | 59 | interface ICloudConfig { 60 | env?: string 61 | traceUser?: boolean 62 | } 63 | 64 | interface IICloudAPI { 65 | init: (config?: IInitCloudConfig) => void 66 | [api: string]: AnyFunction | IAPIFunction 67 | } 68 | 69 | interface ICloudService { 70 | name: string 71 | 72 | getAPIs: () => { [name: string]: IAPIFunction } 73 | } 74 | 75 | interface ICloudServices { 76 | [serviceName: string]: ICloudService 77 | } 78 | 79 | interface ICloudMetaData { 80 | session_id: string 81 | } 82 | 83 | declare class InternalSymbol {} 84 | 85 | interface AnyObject { 86 | [x: string]: any 87 | } 88 | 89 | type AnyArray = any[] 90 | 91 | type AnyFunction = (...args: any[]) => any 92 | 93 | /** 94 | * extend wx with cloud 95 | */ 96 | interface WxCloud { 97 | init: (config?: ICloudConfig) => void 98 | 99 | callFunction(param: OQ): void 100 | callFunction( 101 | param: RQ 102 | ): Promise 103 | 104 | uploadFile(param: OQ): WechatMiniprogram.UploadTask 105 | uploadFile( 106 | param: RQ 107 | ): Promise 108 | 109 | downloadFile( 110 | param: OQ 111 | ): WechatMiniprogram.DownloadTask 112 | downloadFile( 113 | param: RQ 114 | ): Promise 115 | 116 | getTempFileURL(param: OQ): void 117 | getTempFileURL( 118 | param: RQ 119 | ): Promise 120 | 121 | deleteFile(param: OQ): void 122 | deleteFile( 123 | param: RQ 124 | ): Promise 125 | 126 | database: (config?: ICloudConfig) => DB.Database 127 | 128 | CloudID: ICloud.ICloudIDConstructor 129 | CDN: ICloud.ICDNConstructor 130 | 131 | callContainer(param: OQ): void 132 | callContainer( 133 | param: RQ 134 | ): Promise 135 | 136 | connectContainer(param: OQ): void 137 | connectContainer( 138 | param: RQ 139 | ): Promise 140 | 141 | services: ICloud.CloudServices 142 | extend: ICloud.ICloudExtendServices 143 | } 144 | 145 | declare namespace ICloud { 146 | interface ICloudAPIParam extends IAPIParam { 147 | config?: ICloudConfig 148 | } 149 | 150 | // === API: callFunction === 151 | type CallFunctionData = AnyObject 152 | 153 | interface CallFunctionResult extends IAPISuccessParam { 154 | result: AnyObject | string | undefined 155 | } 156 | 157 | interface CallFunctionParam extends ICloudAPIParam { 158 | name: string 159 | data?: CallFunctionData 160 | slow?: boolean 161 | } 162 | // === end === 163 | 164 | // === API: container === 165 | type CallContainerData = AnyObject 166 | 167 | interface CallContainerResult extends IAPISuccessParam { 168 | data: any 169 | statusCode: number 170 | header: Record 171 | callID: string 172 | } 173 | 174 | interface CallContainerParam extends ICloudAPIParam { 175 | path: string 176 | service?: string 177 | method?: string 178 | header?: Record 179 | data?: any // string, object, ArrayBuffer 180 | dataType?: string 181 | responseType?: string 182 | timeout?: number 183 | verbose?: boolean 184 | followRedirect?: boolean 185 | } 186 | 187 | interface ConnectContainerResult extends IAPISuccessParam { 188 | socketTask: WechatMiniprogram.SocketTask 189 | } 190 | 191 | interface ConnectSocketOptions extends IAPIParam { 192 | header?: Record 193 | protocols?: string[] 194 | tcpNoDelay?: boolean 195 | perMessageDeflate?: boolean 196 | timeout?: number 197 | } 198 | 199 | type ConnectContainerParam = Omit< 200 | ConnectSocketOptions, 201 | 'success' | 'fail' | 'complete' 202 | > & 203 | ICloudAPIParam & { 204 | service: string 205 | path?: string 206 | } 207 | // === end === 208 | 209 | // === API: services === 210 | type AsyncSession = T | PromiseLike 211 | interface GatewayCallOptions { 212 | path: string 213 | data: any 214 | shouldSerialize?: boolean 215 | apiVersion?: number 216 | } 217 | interface GatewayInstance { 218 | call: ( 219 | param: CallContainerParam & GatewayCallOptions 220 | ) => Promise 221 | refresh: (session: AsyncSession) => Promise 222 | } 223 | interface GatewayConstructOptions { 224 | id?: string 225 | appid?: string 226 | domain?: string 227 | keepalive?: boolean 228 | prefetch?: boolean 229 | prefetchOptions?: { 230 | concurrent?: number 231 | enableQuic?: boolean 232 | enableHttp2?: boolean 233 | } 234 | } 235 | interface CloudServices { 236 | Gateway: (opts: GatewayConstructOptions) => GatewayInstance 237 | } 238 | // === end === 239 | 240 | // === API: extend === 241 | interface ICloudExtendServices { 242 | AI: ICloudAI 243 | } 244 | interface ICloudAI { 245 | createModel: (modelName: string) => ICloudAIModel 246 | bot: ICloudBot 247 | } 248 | interface ICloudAICallbackOptions { 249 | onEvent?: (ev: ICloudAIEvent) => void 250 | onText?: (text: string) => void 251 | onFinish?: (text: string) => void 252 | } 253 | interface ICloudBot { 254 | get: ({ botId: string }: any) => any 255 | list: (data: any) => any 256 | create: (data: any) => any 257 | update: (data: any) => any 258 | delete: (data: any) => any 259 | getChatRecords: (data: any) => any 260 | sendFeedback: (data: any) => any 261 | getFeedBack: (data: any) => any 262 | getRecommendQuestions: (options: ICloudAICallbackOptions & ICloudBotOptions) => Promise 263 | sendMessage: (options: ICloudAICallbackOptions & ICloudBotOptions) => Promise 264 | } 265 | 266 | interface ICloudBotOptions { data: any, botId: string, timeout?: number } 267 | interface ICloudAIModel { 268 | streamText: (opts: ICloudAIOptions & ICloudAICallbackOptions) => Promise 269 | generateText: (opts: ICloudAIOptions & ICloudAICallbackOptions) => Promise 270 | } 271 | interface ICloudAIChatMessage { 272 | role: 'user' | 'assistant' | string 273 | content: string 274 | } 275 | interface ICloudAIChatModelInput { 276 | model: string 277 | messages: ICloudAIChatMessage[] 278 | temperature?: number 279 | top_p?: number 280 | } 281 | interface ICloudAIOptions{ 282 | data: ICloudAIChatModelInput 283 | } 284 | interface ICloudAIEvent { 285 | event: string 286 | id: string 287 | data: string 288 | json?: any 289 | } 290 | 291 | interface AsyncIterator { 292 | next(value?: any): Promise> 293 | return?(value?: any): Promise> 294 | throw?(e?: any): Promise> 295 | [Symbol.asyncIterator](): AsyncIterableIterator 296 | } 297 | interface ICloudAIStreamResult { 298 | textStream: AsyncIterator 299 | eventStream: AsyncIterator 300 | abort?: () => void 301 | } 302 | // === end === 303 | 304 | // === API: uploadFile === 305 | interface UploadFileResult extends IAPISuccessParam { 306 | fileID: string 307 | statusCode: number 308 | } 309 | 310 | interface UploadFileParam extends ICloudAPIParam { 311 | cloudPath: string 312 | filePath: string 313 | header?: AnyObject 314 | } 315 | // === end === 316 | 317 | // === API: downloadFile === 318 | interface DownloadFileResult extends IAPISuccessParam { 319 | tempFilePath: string 320 | statusCode: number 321 | } 322 | 323 | interface DownloadFileParam extends ICloudAPIParam { 324 | fileID: string 325 | cloudPath?: string 326 | } 327 | // === end === 328 | 329 | // === API: getTempFileURL === 330 | interface GetTempFileURLResult extends IAPISuccessParam { 331 | fileList: GetTempFileURLResultItem[] 332 | } 333 | 334 | interface GetTempFileURLResultItem { 335 | fileID: string 336 | tempFileURL: string 337 | maxAge: number 338 | status: number 339 | errMsg: string 340 | } 341 | 342 | interface GetTempFileURLParam extends ICloudAPIParam { 343 | fileList: string[] 344 | } 345 | // === end === 346 | 347 | // === API: deleteFile === 348 | interface DeleteFileResult extends IAPISuccessParam { 349 | fileList: DeleteFileResultItem[] 350 | } 351 | 352 | interface DeleteFileResultItem { 353 | fileID: string 354 | status: number 355 | errMsg: string 356 | } 357 | 358 | interface DeleteFileParam extends ICloudAPIParam { 359 | fileList: string[] 360 | } 361 | // === end === 362 | 363 | // === API: CloudID === 364 | abstract class CloudID { 365 | constructor(cloudID: string) 366 | } 367 | 368 | interface ICloudIDConstructor { 369 | new (cloudId: string): CloudID 370 | (cloudId: string): CloudID 371 | } 372 | // === end === 373 | 374 | // === API: CDN === 375 | abstract class CDN { 376 | target: string | ArrayBuffer | ICDNFilePathSpec 377 | constructor(target: string | ArrayBuffer | ICDNFilePathSpec) 378 | } 379 | 380 | interface ICDNFilePathSpec { 381 | type: 'filePath' 382 | filePath: string 383 | } 384 | 385 | interface ICDNConstructor { 386 | new (options: string | ArrayBuffer | ICDNFilePathSpec): CDN 387 | (options: string | ArrayBuffer | ICDNFilePathSpec): CDN 388 | } 389 | // === end === 390 | } 391 | 392 | // === Database === 393 | declare namespace DB { 394 | /** 395 | * The class of all exposed cloud database instances 396 | */ 397 | class Database { 398 | readonly config: ICloudConfig 399 | readonly command: DatabaseCommand 400 | readonly Geo: IGeo 401 | readonly serverDate: () => ServerDate 402 | readonly RegExp: IRegExpConstructor 403 | 404 | private constructor() 405 | 406 | collection(collectionName: string): CollectionReference 407 | } 408 | 409 | interface Aggregate { 410 | /** 411 | * @description 聚合阶段。添加新字段到输出的记录。经过 addFields 聚合阶段,输出的所有记录中除了输入时带有的字段外,还将带有 addFields 指定的字段。 412 | */ 413 | addFields(object: any): Aggregate 414 | /** 415 | * @description 聚合阶段。将输入记录根据给定的条件和边界划分成不同的组,每组即一个 bucket。 416 | */ 417 | bucket(object: any): Aggregate 418 | /** 419 | * @description 聚合阶段。将输入记录根据给定的条件划分成不同的组,每组即一个 bucket。与 bucket 的其中一个不同之处在于无需指定 boundaries,bucketAuto 会自动尝试将记录尽可能平均地分散到每组中。 420 | */ 421 | bucketAuto(object: any): Aggregate 422 | /** 423 | * @description 聚合阶段。计算上一聚合阶段输入到本阶段的记录数,输出一个记录,其中指定字段的值为记录数。 424 | */ 425 | count(fieldName: string): Aggregate 426 | /** 427 | * @description 标志聚合操作定义完成,发起实际聚合操作。 428 | */ 429 | end(): Promise 430 | /** 431 | * @description 聚合阶段。将记录按照离给定点从近到远输出。 432 | */ 433 | geoNear(object: any): Aggregate 434 | /** 435 | * @description 聚合阶段。将输入记录按给定表达式分组,输出时每个记录代表一个分组,每个记录的 _id 是区分不同组的 key。输出记录中也可以包括累计值,将输出字段设为累计值即会从该分组中计算累计值。 436 | */ 437 | group(object: any): Aggregate 438 | /** 439 | * @description 聚合阶段。限制输出到下一阶段的记录数。 440 | */ 441 | limit(value: number): Aggregate 442 | /** 443 | * @description 聚合阶段。聚合阶段。联表查询。与同个数据库下的一个指定的集合做 left outer join(左外连接)。对该阶段的每一个输入记录,lookup 会在该记录中增加一个数组字段,该数组是被联表中满足匹配条件的记录列表。lookup 会将连接后的结果输出给下个阶段。 444 | */ 445 | lookup(object: any): Aggregate 446 | /** 447 | * @description 聚合阶段。根据条件过滤文档,并且把符合条件的文档传递给下一个流水线阶段。 448 | */ 449 | match(object: any): Aggregate 450 | /** 451 | * @description 聚合阶段。把指定的字段传递给下一个流水线,指定的字段可以是某个已经存在的字段,也可以是计算出来的新字段。 452 | */ 453 | project(object: any): Aggregate 454 | /** 455 | * @description 聚合阶段。指定一个已有字段作为输出的根节点,也可以指定一个计算出的新字段作为根节点。 456 | */ 457 | replaceRoot(object: any): Aggregate 458 | /** 459 | * @description 聚合阶段。随机从文档中选取指定数量的记录。 460 | */ 461 | sample(size: number): Aggregate 462 | /** 463 | * @description 聚合阶段。指定一个正整数,跳过对应数量的文档,输出剩下的文档。 464 | */ 465 | skip(value: number): Aggregate 466 | /** 467 | * @description 聚合阶段。根据指定的字段,对输入的文档进行排序。 468 | */ 469 | sort(object: any): Aggregate 470 | /** 471 | * @description 聚合阶段。根据传入的表达式,将传入的集合进行分组(group)。然后计算不同组的数量,并且将这些组按照它们的数量进行排序,返回排序后的结果。 472 | */ 473 | sortByCount(object: any): Aggregate 474 | /** 475 | * @description 聚合阶段。使用指定的数组字段中的每个元素,对文档进行拆分。拆分后,文档会从一个变为一个或多个,分别对应数组的每个元素。 476 | */ 477 | unwind(value: string | object): Aggregate 478 | } 479 | 480 | class CollectionReference extends Query { 481 | readonly collectionName: string 482 | 483 | private constructor(name: string, database: Database) 484 | 485 | doc(docId: string | number): DocumentReference 486 | 487 | add(options: OQ): void 488 | add(options: RQ): Promise 489 | aggregate(): Aggregate 490 | } 491 | 492 | class DocumentReference { 493 | private constructor(docId: string | number, database: Database) 494 | 495 | field(object: Record): this 496 | 497 | get(options: OQ): void 498 | get(options?: RQ): Promise 499 | 500 | set(options: OQ): void 501 | set(options?: RQ): Promise 502 | 503 | update(options: OQ): void 504 | update( 505 | options?: RQ 506 | ): Promise 507 | 508 | remove(options: OQ): void 509 | remove( 510 | options?: RQ 511 | ): Promise 512 | 513 | watch(options: IWatchOptions): RealtimeListener 514 | } 515 | 516 | class RealtimeListener { 517 | // "And Now His Watch Is Ended" 518 | close: () => Promise 519 | } 520 | 521 | class Query { 522 | where(condition: IQueryCondition): Query 523 | 524 | orderBy(fieldPath: string, order: string): Query 525 | 526 | limit(max: number): Query 527 | 528 | skip(offset: number): Query 529 | 530 | field(object: Record): Query 531 | 532 | get(options: OQ): void 533 | get(options?: RQ): Promise 534 | 535 | count(options: OQ): void 536 | count(options?: RQ): Promise 537 | 538 | watch(options: IWatchOptions): RealtimeListener 539 | } 540 | 541 | interface DatabaseCommand { 542 | eq(val: any): DatabaseQueryCommand 543 | neq(val: any): DatabaseQueryCommand 544 | gt(val: any): DatabaseQueryCommand 545 | gte(val: any): DatabaseQueryCommand 546 | lt(val: any): DatabaseQueryCommand 547 | lte(val: any): DatabaseQueryCommand 548 | in(val: any[]): DatabaseQueryCommand 549 | nin(val: any[]): DatabaseQueryCommand 550 | 551 | geoNear(options: IGeoNearCommandOptions): DatabaseQueryCommand 552 | geoWithin(options: IGeoWithinCommandOptions): DatabaseQueryCommand 553 | geoIntersects( 554 | options: IGeoIntersectsCommandOptions 555 | ): DatabaseQueryCommand 556 | 557 | and( 558 | ...expressions: Array 559 | ): DatabaseLogicCommand 560 | or( 561 | ...expressions: Array 562 | ): DatabaseLogicCommand 563 | nor( 564 | ...expressions: Array 565 | ): DatabaseLogicCommand 566 | not(expression: DatabaseLogicCommand): DatabaseLogicCommand 567 | 568 | exists(val: boolean): DatabaseQueryCommand 569 | 570 | mod(divisor: number, remainder: number): DatabaseQueryCommand 571 | 572 | all(val: any[]): DatabaseQueryCommand 573 | elemMatch(val: any): DatabaseQueryCommand 574 | size(val: number): DatabaseQueryCommand 575 | 576 | set(val: any): DatabaseUpdateCommand 577 | remove(): DatabaseUpdateCommand 578 | inc(val: number): DatabaseUpdateCommand 579 | mul(val: number): DatabaseUpdateCommand 580 | min(val: number): DatabaseUpdateCommand 581 | max(val: number): DatabaseUpdateCommand 582 | rename(val: string): DatabaseUpdateCommand 583 | bit(val: number): DatabaseUpdateCommand 584 | 585 | push(...values: any[]): DatabaseUpdateCommand 586 | pop(): DatabaseUpdateCommand 587 | shift(): DatabaseUpdateCommand 588 | unshift(...values: any[]): DatabaseUpdateCommand 589 | addToSet(val: any): DatabaseUpdateCommand 590 | pull(val: any): DatabaseUpdateCommand 591 | pullAll(val: any): DatabaseUpdateCommand 592 | 593 | project: { 594 | slice(val: number | [number, number]): DatabaseProjectionCommand 595 | } 596 | 597 | aggregate: { 598 | __safe_props__?: Set 599 | 600 | abs(val: any): DatabaseAggregateCommand 601 | add(val: any): DatabaseAggregateCommand 602 | addToSet(val: any): DatabaseAggregateCommand 603 | allElementsTrue(val: any): DatabaseAggregateCommand 604 | and(val: any): DatabaseAggregateCommand 605 | anyElementTrue(val: any): DatabaseAggregateCommand 606 | arrayElemAt(val: any): DatabaseAggregateCommand 607 | arrayToObject(val: any): DatabaseAggregateCommand 608 | avg(val: any): DatabaseAggregateCommand 609 | ceil(val: any): DatabaseAggregateCommand 610 | cmp(val: any): DatabaseAggregateCommand 611 | concat(val: any): DatabaseAggregateCommand 612 | concatArrays(val: any): DatabaseAggregateCommand 613 | cond(val: any): DatabaseAggregateCommand 614 | convert(val: any): DatabaseAggregateCommand 615 | dateFromParts(val: any): DatabaseAggregateCommand 616 | dateToParts(val: any): DatabaseAggregateCommand 617 | dateFromString(val: any): DatabaseAggregateCommand 618 | dateToString(val: any): DatabaseAggregateCommand 619 | dayOfMonth(val: any): DatabaseAggregateCommand 620 | dayOfWeek(val: any): DatabaseAggregateCommand 621 | dayOfYear(val: any): DatabaseAggregateCommand 622 | divide(val: any): DatabaseAggregateCommand 623 | eq(val: any): DatabaseAggregateCommand 624 | exp(val: any): DatabaseAggregateCommand 625 | filter(val: any): DatabaseAggregateCommand 626 | first(val: any): DatabaseAggregateCommand 627 | floor(val: any): DatabaseAggregateCommand 628 | gt(val: any): DatabaseAggregateCommand 629 | gte(val: any): DatabaseAggregateCommand 630 | hour(val: any): DatabaseAggregateCommand 631 | ifNull(val: any): DatabaseAggregateCommand 632 | in(val: any): DatabaseAggregateCommand 633 | indexOfArray(val: any): DatabaseAggregateCommand 634 | indexOfBytes(val: any): DatabaseAggregateCommand 635 | indexOfCP(val: any): DatabaseAggregateCommand 636 | isArray(val: any): DatabaseAggregateCommand 637 | isoDayOfWeek(val: any): DatabaseAggregateCommand 638 | isoWeek(val: any): DatabaseAggregateCommand 639 | isoWeekYear(val: any): DatabaseAggregateCommand 640 | last(val: any): DatabaseAggregateCommand 641 | let(val: any): DatabaseAggregateCommand 642 | literal(val: any): DatabaseAggregateCommand 643 | ln(val: any): DatabaseAggregateCommand 644 | log(val: any): DatabaseAggregateCommand 645 | log10(val: any): DatabaseAggregateCommand 646 | lt(val: any): DatabaseAggregateCommand 647 | lte(val: any): DatabaseAggregateCommand 648 | ltrim(val: any): DatabaseAggregateCommand 649 | map(val: any): DatabaseAggregateCommand 650 | max(val: any): DatabaseAggregateCommand 651 | mergeObjects(val: any): DatabaseAggregateCommand 652 | meta(val: any): DatabaseAggregateCommand 653 | min(val: any): DatabaseAggregateCommand 654 | millisecond(val: any): DatabaseAggregateCommand 655 | minute(val: any): DatabaseAggregateCommand 656 | mod(val: any): DatabaseAggregateCommand 657 | month(val: any): DatabaseAggregateCommand 658 | multiply(val: any): DatabaseAggregateCommand 659 | neq(val: any): DatabaseAggregateCommand 660 | not(val: any): DatabaseAggregateCommand 661 | objectToArray(val: any): DatabaseAggregateCommand 662 | or(val: any): DatabaseAggregateCommand 663 | pow(val: any): DatabaseAggregateCommand 664 | push(val: any): DatabaseAggregateCommand 665 | range(val: any): DatabaseAggregateCommand 666 | reduce(val: any): DatabaseAggregateCommand 667 | reverseArray(val: any): DatabaseAggregateCommand 668 | rtrim(val: any): DatabaseAggregateCommand 669 | second(val: any): DatabaseAggregateCommand 670 | setDifference(val: any): DatabaseAggregateCommand 671 | setEquals(val: any): DatabaseAggregateCommand 672 | setIntersection(val: any): DatabaseAggregateCommand 673 | setIsSubset(val: any): DatabaseAggregateCommand 674 | setUnion(val: any): DatabaseAggregateCommand 675 | size(val: any): DatabaseAggregateCommand 676 | slice(val: any): DatabaseAggregateCommand 677 | split(val: any): DatabaseAggregateCommand 678 | sqrt(val: any): DatabaseAggregateCommand 679 | stdDevPop(val: any): DatabaseAggregateCommand 680 | stdDevSamp(val: any): DatabaseAggregateCommand 681 | strcasecmp(val: any): DatabaseAggregateCommand 682 | strLenBytes(val: any): DatabaseAggregateCommand 683 | strLenCP(val: any): DatabaseAggregateCommand 684 | substr(val: any): DatabaseAggregateCommand 685 | substrBytes(val: any): DatabaseAggregateCommand 686 | substrCP(val: any): DatabaseAggregateCommand 687 | subtract(val: any): DatabaseAggregateCommand 688 | sum(val: any): DatabaseAggregateCommand 689 | switch(val: any): DatabaseAggregateCommand 690 | toBool(val: any): DatabaseAggregateCommand 691 | toDate(val: any): DatabaseAggregateCommand 692 | toDecimal(val: any): DatabaseAggregateCommand 693 | toDouble(val: any): DatabaseAggregateCommand 694 | toInt(val: any): DatabaseAggregateCommand 695 | toLong(val: any): DatabaseAggregateCommand 696 | toObjectId(val: any): DatabaseAggregateCommand 697 | toString(val: any): DatabaseAggregateCommand 698 | toLower(val: any): DatabaseAggregateCommand 699 | toUpper(val: any): DatabaseAggregateCommand 700 | trim(val: any): DatabaseAggregateCommand 701 | trunc(val: any): DatabaseAggregateCommand 702 | type(val: any): DatabaseAggregateCommand 703 | week(val: any): DatabaseAggregateCommand 704 | year(val: any): DatabaseAggregateCommand 705 | zip(val: any): DatabaseAggregateCommand 706 | } 707 | } 708 | 709 | class DatabaseAggregateCommand {} 710 | 711 | enum LOGIC_COMMANDS_LITERAL { 712 | AND = 'and', 713 | OR = 'or', 714 | NOT = 'not', 715 | NOR = 'nor' 716 | } 717 | 718 | class DatabaseLogicCommand { 719 | and(...expressions: DatabaseLogicCommand[]): DatabaseLogicCommand 720 | or(...expressions: DatabaseLogicCommand[]): DatabaseLogicCommand 721 | nor(...expressions: DatabaseLogicCommand[]): DatabaseLogicCommand 722 | not(expression: DatabaseLogicCommand): DatabaseLogicCommand 723 | } 724 | 725 | enum QUERY_COMMANDS_LITERAL { 726 | // comparison 727 | EQ = 'eq', 728 | NEQ = 'neq', 729 | GT = 'gt', 730 | GTE = 'gte', 731 | LT = 'lt', 732 | LTE = 'lte', 733 | IN = 'in', 734 | NIN = 'nin', 735 | // geo 736 | GEO_NEAR = 'geoNear', 737 | GEO_WITHIN = 'geoWithin', 738 | GEO_INTERSECTS = 'geoIntersects', 739 | // element 740 | EXISTS = 'exists', 741 | // evaluation 742 | MOD = 'mod', 743 | // array 744 | ALL = 'all', 745 | ELEM_MATCH = 'elemMatch', 746 | SIZE = 'size' 747 | } 748 | 749 | class DatabaseQueryCommand extends DatabaseLogicCommand { 750 | eq(val: any): DatabaseLogicCommand 751 | neq(val: any): DatabaseLogicCommand 752 | gt(val: any): DatabaseLogicCommand 753 | gte(val: any): DatabaseLogicCommand 754 | lt(val: any): DatabaseLogicCommand 755 | lte(val: any): DatabaseLogicCommand 756 | in(val: any[]): DatabaseLogicCommand 757 | nin(val: any[]): DatabaseLogicCommand 758 | 759 | exists(val: boolean): DatabaseLogicCommand 760 | 761 | mod(divisor: number, remainder: number): DatabaseLogicCommand 762 | 763 | all(val: any[]): DatabaseLogicCommand 764 | elemMatch(val: any): DatabaseLogicCommand 765 | size(val: number): DatabaseLogicCommand 766 | 767 | geoNear(options: IGeoNearCommandOptions): DatabaseLogicCommand 768 | geoWithin(options: IGeoWithinCommandOptions): DatabaseLogicCommand 769 | geoIntersects( 770 | options: IGeoIntersectsCommandOptions 771 | ): DatabaseLogicCommand 772 | } 773 | 774 | enum PROJECTION_COMMANDS_LITERAL { 775 | SLICE = 'slice' 776 | } 777 | 778 | class DatabaseProjectionCommand {} 779 | 780 | enum UPDATE_COMMANDS_LITERAL { 781 | // field 782 | SET = 'set', 783 | REMOVE = 'remove', 784 | INC = 'inc', 785 | MUL = 'mul', 786 | MIN = 'min', 787 | MAX = 'max', 788 | RENAME = 'rename', 789 | // bitwise 790 | BIT = 'bit', 791 | // array 792 | PUSH = 'push', 793 | POP = 'pop', 794 | SHIFT = 'shift', 795 | UNSHIFT = 'unshift', 796 | ADD_TO_SET = 'addToSet', 797 | PULL = 'pull', 798 | PULL_ALL = 'pullAll' 799 | } 800 | 801 | class DatabaseUpdateCommand {} 802 | 803 | class Batch {} 804 | 805 | /** 806 | * A contract that all API provider must adhere to 807 | */ 808 | class APIBaseContract< 809 | PromiseReturn, 810 | CallbackReturn, 811 | Param extends IAPIParam, 812 | Context = any 813 | > { 814 | getContext(param: Param): Context 815 | 816 | /** 817 | * In case of callback-style invocation, this function will be called 818 | */ 819 | getCallbackReturn(param: Param, context: Context): CallbackReturn 820 | 821 | getFinalParam(param: Param, context: Context): T 822 | 823 | run(param: T): Promise 824 | } 825 | 826 | interface IGeoPointConstructor { 827 | new (longitude: number, latitide: number): GeoPoint 828 | new (geojson: IGeoJSONPoint): GeoPoint 829 | (longitude: number, latitide: number): GeoPoint 830 | (geojson: IGeoJSONPoint): GeoPoint 831 | } 832 | 833 | interface IGeoMultiPointConstructor { 834 | new (points: GeoPoint[] | IGeoJSONMultiPoint): GeoMultiPoint 835 | (points: GeoPoint[] | IGeoJSONMultiPoint): GeoMultiPoint 836 | } 837 | 838 | interface IGeoLineStringConstructor { 839 | new (points: GeoPoint[] | IGeoJSONLineString): GeoLineString 840 | (points: GeoPoint[] | IGeoJSONLineString): GeoLineString 841 | } 842 | 843 | interface IGeoMultiLineStringConstructor { 844 | new ( 845 | lineStrings: GeoLineString[] | IGeoJSONMultiLineString 846 | ): GeoMultiLineString 847 | ( 848 | lineStrings: GeoLineString[] | IGeoJSONMultiLineString 849 | ): GeoMultiLineString 850 | } 851 | 852 | interface IGeoPolygonConstructor { 853 | new (lineStrings: GeoLineString[] | IGeoJSONPolygon): GeoPolygon 854 | (lineStrings: GeoLineString[] | IGeoJSONPolygon): GeoPolygon 855 | } 856 | 857 | interface IGeoMultiPolygonConstructor { 858 | new (polygons: GeoPolygon[] | IGeoJSONMultiPolygon): GeoMultiPolygon 859 | (polygons: GeoPolygon[] | IGeoJSONMultiPolygon): GeoMultiPolygon 860 | } 861 | 862 | interface IGeo { 863 | Point: IGeoPointConstructor 864 | MultiPoint: IGeoMultiPointConstructor 865 | LineString: IGeoLineStringConstructor 866 | MultiLineString: IGeoMultiLineStringConstructor 867 | Polygon: IGeoPolygonConstructor 868 | MultiPolygon: IGeoMultiPolygonConstructor 869 | } 870 | 871 | interface IGeoJSONPoint { 872 | type: 'Point' 873 | coordinates: [number, number] 874 | } 875 | 876 | interface IGeoJSONMultiPoint { 877 | type: 'MultiPoint' 878 | coordinates: Array<[number, number]> 879 | } 880 | 881 | interface IGeoJSONLineString { 882 | type: 'LineString' 883 | coordinates: Array<[number, number]> 884 | } 885 | 886 | interface IGeoJSONMultiLineString { 887 | type: 'MultiLineString' 888 | coordinates: Array> 889 | } 890 | 891 | interface IGeoJSONPolygon { 892 | type: 'Polygon' 893 | coordinates: Array> 894 | } 895 | 896 | interface IGeoJSONMultiPolygon { 897 | type: 'MultiPolygon' 898 | coordinates: Array>> 899 | } 900 | 901 | type IGeoJSONObject = 902 | | IGeoJSONPoint 903 | | IGeoJSONMultiPoint 904 | | IGeoJSONLineString 905 | | IGeoJSONMultiLineString 906 | | IGeoJSONPolygon 907 | | IGeoJSONMultiPolygon 908 | 909 | abstract class GeoPoint { 910 | longitude: number 911 | latitude: number 912 | 913 | constructor(longitude: number, latitude: number) 914 | 915 | toJSON(): Record 916 | toString(): string 917 | } 918 | 919 | abstract class GeoMultiPoint { 920 | points: GeoPoint[] 921 | 922 | constructor(points: GeoPoint[]) 923 | 924 | toJSON(): IGeoJSONMultiPoint 925 | toString(): string 926 | } 927 | 928 | abstract class GeoLineString { 929 | points: GeoPoint[] 930 | 931 | constructor(points: GeoPoint[]) 932 | 933 | toJSON(): IGeoJSONLineString 934 | toString(): string 935 | } 936 | 937 | abstract class GeoMultiLineString { 938 | lines: GeoLineString[] 939 | 940 | constructor(lines: GeoLineString[]) 941 | 942 | toJSON(): IGeoJSONMultiLineString 943 | toString(): string 944 | } 945 | 946 | abstract class GeoPolygon { 947 | lines: GeoLineString[] 948 | 949 | constructor(lines: GeoLineString[]) 950 | 951 | toJSON(): IGeoJSONPolygon 952 | toString(): string 953 | } 954 | 955 | abstract class GeoMultiPolygon { 956 | polygons: GeoPolygon[] 957 | 958 | constructor(polygons: GeoPolygon[]) 959 | 960 | toJSON(): IGeoJSONMultiPolygon 961 | toString(): string 962 | } 963 | 964 | type GeoInstance = 965 | | GeoPoint 966 | | GeoMultiPoint 967 | | GeoLineString 968 | | GeoMultiLineString 969 | | GeoPolygon 970 | | GeoMultiPolygon 971 | 972 | interface IGeoNearCommandOptions { 973 | geometry: GeoPoint 974 | maxDistance?: number 975 | minDistance?: number 976 | } 977 | 978 | interface IGeoWithinCommandOptions { 979 | geometry: GeoPolygon | GeoMultiPolygon 980 | } 981 | 982 | interface IGeoIntersectsCommandOptions { 983 | geometry: 984 | | GeoPoint 985 | | GeoMultiPoint 986 | | GeoLineString 987 | | GeoMultiLineString 988 | | GeoPolygon 989 | | GeoMultiPolygon 990 | } 991 | 992 | interface IServerDateOptions { 993 | offset: number 994 | } 995 | 996 | abstract class ServerDate { 997 | readonly options: IServerDateOptions 998 | constructor(options?: IServerDateOptions) 999 | } 1000 | 1001 | interface IRegExpOptions { 1002 | regexp: string 1003 | options?: string 1004 | } 1005 | 1006 | interface IRegExpConstructor { 1007 | new (options: IRegExpOptions): RegExp 1008 | (options: IRegExpOptions): RegExp 1009 | } 1010 | 1011 | abstract class RegExp { 1012 | readonly regexp: string 1013 | readonly options: string 1014 | constructor(options: IRegExpOptions) 1015 | } 1016 | 1017 | type DocumentId = string | number 1018 | 1019 | interface IDocumentData { 1020 | _id?: DocumentId 1021 | [key: string]: any 1022 | } 1023 | 1024 | type IDBAPIParam = IAPIParam 1025 | 1026 | interface IAddDocumentOptions extends IDBAPIParam { 1027 | data: IDocumentData 1028 | } 1029 | 1030 | type IGetDocumentOptions = IDBAPIParam 1031 | 1032 | type ICountDocumentOptions = IDBAPIParam 1033 | 1034 | interface IUpdateDocumentOptions extends IDBAPIParam { 1035 | data: IUpdateCondition 1036 | } 1037 | 1038 | interface IUpdateSingleDocumentOptions extends IDBAPIParam { 1039 | data: IUpdateCondition 1040 | } 1041 | 1042 | interface ISetDocumentOptions extends IDBAPIParam { 1043 | data: IUpdateCondition 1044 | } 1045 | 1046 | interface ISetSingleDocumentOptions extends IDBAPIParam { 1047 | data: IUpdateCondition 1048 | } 1049 | 1050 | interface IRemoveDocumentOptions extends IDBAPIParam { 1051 | query: IQueryCondition 1052 | } 1053 | 1054 | type IRemoveSingleDocumentOptions = IDBAPIParam 1055 | 1056 | interface IWatchOptions { 1057 | // server realtime data init & change event 1058 | onChange: (snapshot: ISnapshot) => void 1059 | // error while connecting / listening 1060 | onError: (error: any) => void 1061 | } 1062 | 1063 | interface ISnapshot { 1064 | id: number 1065 | docChanges: ISingleDBEvent[] 1066 | docs: Record 1067 | type?: SnapshotType 1068 | } 1069 | 1070 | type SnapshotType = 'init' 1071 | 1072 | interface ISingleDBEvent { 1073 | id: number 1074 | dataType: DataType 1075 | queueType: QueueType 1076 | docId: string 1077 | doc: Record 1078 | updatedFields?: Record 1079 | removedFields?: string[] 1080 | } 1081 | 1082 | type DataType = 'init' | 'update' | 'replace' | 'add' | 'remove' | 'limit' 1083 | 1084 | type QueueType = 'init' | 'enqueue' | 'dequeue' | 'update' 1085 | 1086 | interface IQueryCondition { 1087 | [key: string]: any 1088 | } 1089 | 1090 | type IStringQueryCondition = string 1091 | 1092 | interface IQueryResult extends IAPISuccessParam { 1093 | data: IDocumentData[] 1094 | } 1095 | 1096 | interface IQuerySingleResult extends IAPISuccessParam { 1097 | data: IDocumentData 1098 | } 1099 | 1100 | interface IUpdateCondition { 1101 | [key: string]: any 1102 | } 1103 | 1104 | type IStringUpdateCondition = string 1105 | 1106 | interface IAddResult extends IAPISuccessParam { 1107 | _id: DocumentId 1108 | } 1109 | 1110 | interface IUpdateResult extends IAPISuccessParam { 1111 | stats: { 1112 | updated: number 1113 | // created: number, 1114 | } 1115 | } 1116 | 1117 | interface ISetResult extends IAPISuccessParam { 1118 | _id: DocumentId 1119 | stats: { 1120 | updated: number 1121 | created: number 1122 | } 1123 | } 1124 | 1125 | interface IRemoveResult extends IAPISuccessParam { 1126 | stats: { 1127 | removed: number 1128 | } 1129 | } 1130 | 1131 | interface ICountResult extends IAPISuccessParam { 1132 | total: number 1133 | } 1134 | } 1135 | 1136 | type Optional = { [K in keyof T]+?: T[K] } 1137 | 1138 | type OQ< 1139 | T extends Optional< 1140 | Record<'complete' | 'success' | 'fail', (...args: any[]) => any> 1141 | > 1142 | > = 1143 | | (RQ & Required>) 1144 | | (RQ & Required>) 1145 | | (RQ & Required>) 1146 | | (RQ & Required>) 1147 | | (RQ & Required>) 1148 | | (RQ & Required>) 1149 | | (RQ & Required>) 1150 | 1151 | type RQ< 1152 | T extends Optional< 1153 | Record<'complete' | 'success' | 'fail', (...args: any[]) => any> 1154 | > 1155 | > = Pick> 1156 | -------------------------------------------------------------------------------- /types/wx/lib.wx.component.d.ts: -------------------------------------------------------------------------------- 1 | /*! ***************************************************************************** 2 | Copyright (c) 2025 Tencent, Inc. All rights reserved. 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | this software and associated documentation files (the "Software"), to deal in 6 | the Software without restriction, including without limitation the rights to 7 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 8 | of the Software, and to permit persons to whom the Software is furnished to do 9 | so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | SOFTWARE. 21 | ***************************************************************************** */ 22 | 23 | declare namespace WechatMiniprogram.Component { 24 | type FilterUnknownType = string extends keyof T ? {} : T 25 | type Instance< 26 | TData extends DataOption, 27 | TProperty extends PropertyOption, 28 | TMethod extends Partial, 29 | TBehavior extends BehaviorOption, 30 | TCustomInstanceProperty extends IAnyObject = {}, 31 | TIsPage extends boolean = false 32 | > = InstanceProperties & 33 | InstanceMethods & 34 | TMethod & 35 | MixinMethods & 36 | (TIsPage extends true ? Page.ILifetime : {}) & 37 | Omit & { 38 | /** 组件数据,**包括内部数据和属性值** */ 39 | data: FilterUnknownType & MixinData & 40 | MixinProperties & PropertyOptionToData> 41 | /** 组件数据,**包括内部数据和属性值**(与 `data` 一致) */ 42 | properties: FilterUnknownType & MixinData & 43 | MixinProperties & PropertyOptionToData> 44 | } 45 | 46 | type IEmptyArray = [] 47 | type TrivialInstance = Instance< 48 | IAnyObject, 49 | IAnyObject, 50 | IAnyObject, 51 | IEmptyArray, 52 | IAnyObject 53 | > 54 | type TrivialOption = Options 55 | type Options< 56 | TData extends DataOption, 57 | TProperty extends PropertyOption, 58 | TMethod extends MethodOption, 59 | TBehavior extends BehaviorOption, 60 | TCustomInstanceProperty extends IAnyObject = {}, 61 | TIsPage extends boolean = false 62 | > = Partial> & 63 | Partial> & 64 | Partial> & 65 | Partial> & 66 | Partial & 67 | Partial & 68 | ThisType< 69 | Instance< 70 | TData, 71 | TProperty, 72 | TMethod, 73 | TBehavior, 74 | TCustomInstanceProperty, 75 | TIsPage 76 | > 77 | > 78 | interface Constructor { 79 | < 80 | TData extends DataOption, 81 | TProperty extends PropertyOption, 82 | TMethod extends MethodOption, 83 | TBehavior extends BehaviorOption, 84 | TCustomInstanceProperty extends IAnyObject = {}, 85 | TIsPage extends boolean = false 86 | >( 87 | options: Options< 88 | TData, 89 | TProperty, 90 | TMethod, 91 | TBehavior, 92 | TCustomInstanceProperty, 93 | TIsPage 94 | > 95 | ): string 96 | } 97 | type DataOption = Record 98 | type PropertyOption = Record 99 | type MethodOption = Record 100 | 101 | type BehaviorOption = Behavior.BehaviorIdentifier[] 102 | type ExtractBehaviorType = T extends { BehaviorType?: infer B } ? B : never 103 | type ExtractData = T extends { data: infer D } ? D : never 104 | type ExtractProperties = T extends { properties: infer P } ? 105 | TIsBehavior extends true ? P : PropertyOptionToData

: never 106 | type ExtractMethods = T extends { methods: infer M } ? M : never 107 | type UnionToIntersection = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never 108 | type MixinData = UnionToIntersection>> 109 | type MixinProperties = UnionToIntersection, TIsBehavior>> 110 | type MixinMethods = UnionToIntersection>> 111 | 112 | interface Behavior { 113 | /** 类似于mixins和traits的组件间代码复用机制,参见 [behaviors](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/behaviors.html) */ 114 | behaviors?: B 115 | } 116 | 117 | interface Data { 118 | /** 组件的内部数据,和 `properties` 一同用于组件的模板渲染 */ 119 | data?: D 120 | } 121 | interface Property

{ 122 | /** 组件的对外属性,是属性名到属性设置的映射表 */ 123 | properties: P 124 | } 125 | interface Method { 126 | /** 组件的方法,包括事件响应函数和任意的自定义方法,关于事件响应函数的使用,参见 [组件间通信与事件](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/events.html) */ 127 | methods: M & (TIsPage extends true ? Partial : {}) 128 | } 129 | type PropertyType = 130 | | StringConstructor 131 | | NumberConstructor 132 | | BooleanConstructor 133 | | ArrayConstructor 134 | | ObjectConstructor 135 | | null 136 | type ValueType = T extends null 137 | ? any 138 | : T extends StringConstructor 139 | ? string 140 | : T extends NumberConstructor 141 | ? number 142 | : T extends BooleanConstructor 143 | ? boolean 144 | : T extends ArrayConstructor 145 | ? any[] 146 | : T extends ObjectConstructor 147 | ? IAnyObject 148 | : never 149 | type FullProperty = { 150 | /** 属性类型 */ 151 | type: T 152 | /** 属性初始值 */ 153 | value?: ValueType 154 | /** 属性值被更改时的响应函数 */ 155 | observer?: 156 | | string 157 | | (( 158 | newVal: ValueType, 159 | oldVal: ValueType, 160 | changedPath: Array 161 | ) => void) 162 | /** 属性的类型(可以指定多个) */ 163 | optionalTypes?: ShortProperty[] 164 | } 165 | type AllFullProperty = 166 | | FullProperty 167 | | FullProperty 168 | | FullProperty 169 | | FullProperty 170 | | FullProperty 171 | | FullProperty 172 | type ShortProperty = 173 | | StringConstructor 174 | | NumberConstructor 175 | | BooleanConstructor 176 | | ArrayConstructor 177 | | ObjectConstructor 178 | | null 179 | type AllProperty = AllFullProperty | ShortProperty 180 | type PropertyToData = T extends ShortProperty 181 | ? ValueType 182 | : FullPropertyToData> 183 | type ArrayOrObject = ArrayConstructor | ObjectConstructor 184 | type FullPropertyToData = T['type'] extends ArrayOrObject ? unknown extends T['value'] ? ValueType : T['value'] : ValueType 185 | type PropertyOptionToData

= { 186 | [name in keyof P]: PropertyToData 187 | } 188 | 189 | interface Router { 190 | switchTab: Wx['switchTab'] 191 | reLaunch: Wx['reLaunch'] 192 | redirectTo: Wx['redirectTo'] 193 | navigateTo: Wx['navigateTo'] 194 | navigateBack: Wx['navigateBack'] 195 | } 196 | 197 | interface InstanceProperties { 198 | /** 组件的文件路径 */ 199 | is: string 200 | /** 节点id */ 201 | id: string 202 | /** 节点dataset */ 203 | dataset: Record 204 | /** 上一次退出前 onSaveExitState 保存的数据 */ 205 | exitState: any 206 | /** 相对于当前自定义组件的 Router 对象 */ 207 | router: Router 208 | /** 相对于当前自定义组件所在页面的 Router 对象 */ 209 | pageRouter: Router 210 | /** 渲染当前组件的渲染后端 */ 211 | renderer: 'webview' | 'skyline' 212 | } 213 | 214 | interface InstanceMethods { 215 | /** `setData` 函数用于将数据从逻辑层发送到视图层 216 | *(异步),同时改变对应的 `this.data` 的值(同步)。 217 | * 218 | * **注意:** 219 | * 220 | * 1. **直接修改 this.data 而不调用 this.setData 是无法改变页面的状态的,还会造成数据不一致**。 221 | * 1. 仅支持设置可 JSON 化的数据。 222 | * 1. 单次设置的数据不能超过1024kB,请尽量避免一次设置过多的数据。 223 | * 1. 请不要把 data 中任何一项的 value 设为 `undefined` ,否则这一项将不被设置并可能遗留一些潜在问题。 224 | */ 225 | setData( 226 | /** 这次要改变的数据 227 | * 228 | * 以 `key: value` 的形式表示,将 `this.data` 中的 `key` 对应的值改变成 `value`。 229 | * 230 | * 其中 `key` 可以以数据路径的形式给出,支持改变数组中的某一项或对象的某个属性,如 `array[2].message`,`a.b.c.d`,并且不需要在 this.data 中预先定义。 231 | */ 232 | data: Partial & IAnyObject, 233 | /** setData引起的界面更新渲染完毕后的回调函数,最低基础库: `1.5.0` */ 234 | callback?: () => void 235 | ): void 236 | 237 | /** 检查组件是否具有 `behavior` (检查时会递归检查被直接或间接引入的所有behavior) */ 238 | hasBehavior(behavior: Behavior.BehaviorIdentifier): void 239 | /** 触发事件,参见组件事件 */ 240 | triggerEvent( 241 | name: string, 242 | detail?: DetailType, 243 | options?: TriggerEventOption 244 | ): void 245 | /** 创建一个 SelectorQuery 对象,选择器选取范围为这个组件实例内 */ 246 | createSelectorQuery(): SelectorQuery 247 | /** 创建一个 IntersectionObserver 对象,选择器选取范围为这个组件实例内 */ 248 | createIntersectionObserver( 249 | options: CreateIntersectionObserverOption 250 | ): IntersectionObserver 251 | /** 创建一个 MediaQueryObserver 对象 */ 252 | createMediaQueryObserver(): MediaQueryObserver 253 | /** 使用选择器选择组件实例节点,返回匹配到的第一个组件实例对象(会被 `wx://component-export` 影响) */ 254 | selectComponent(selector: string): TrivialInstance 255 | /** 使用选择器选择组件实例节点,返回匹配到的全部组件实例对象组成的数组 */ 256 | selectAllComponents(selector: string): TrivialInstance[] 257 | /** 258 | * 选取当前组件节点所在的组件实例(即组件的引用者),返回它的组件实例对象(会被 `wx://component-export` 影响) 259 | * 260 | * 最低基础库版本:[`2.8.2`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 261 | **/ 262 | selectOwnerComponent(): TrivialInstance 263 | /** 获取这个关系所对应的所有关联节点,参见 组件间关系 */ 264 | getRelationNodes(relationKey: string): TrivialInstance[] 265 | /** 266 | * 立刻执行 callback ,其中的多个 setData 之间不会触发界面绘制(只有某些特殊场景中需要,如用于在不同组件同时 setData 时进行界面绘制同步) 267 | * 268 | * 最低基础库版本:[`2.4.0`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 269 | **/ 270 | groupSetData(callback?: () => void): void 271 | /** 272 | * 返回当前页面的 custom-tab-bar 的组件实例 273 | * 274 | * 最低基础库版本:[`2.6.2`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 275 | **/ 276 | getTabBar(): TrivialInstance 277 | /** 278 | * 返回页面标识符(一个字符串),可以用来判断几个自定义组件实例是不是在同一个页面内 279 | * 280 | * 最低基础库版本:[`2.7.1`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 281 | **/ 282 | getPageId(): string 283 | /** 284 | * 执行关键帧动画,详见[动画](https://developers.weixin.qq.com/miniprogram/dev/framework/view/animation.html) 285 | * 286 | * 最低基础库版本:[`2.9.0`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 287 | **/ 288 | animate( 289 | selector: string, 290 | keyFrames: KeyFrame[], 291 | duration: number, 292 | callback?: () => void 293 | ): void 294 | /** 295 | * 执行关键帧动画,详见[动画](https://developers.weixin.qq.com/miniprogram/dev/framework/view/animation.html) 296 | * 297 | * 最低基础库版本:[`2.9.0`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 298 | **/ 299 | animate( 300 | selector: string, 301 | keyFrames: ScrollTimelineKeyframe[], 302 | duration: number, 303 | scrollTimeline: ScrollTimelineOption 304 | ): void 305 | /** 306 | * 清除关键帧动画,详见[动画](https://developers.weixin.qq.com/miniprogram/dev/framework/view/animation.html) 307 | * 308 | * 最低基础库版本:[`2.9.0`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 309 | **/ 310 | clearAnimation(selector: string, callback: () => void): void 311 | /** 312 | * 清除关键帧动画,详见[动画](https://developers.weixin.qq.com/miniprogram/dev/framework/view/animation.html) 313 | * 314 | * 最低基础库版本:[`2.9.0`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 315 | **/ 316 | clearAnimation( 317 | selector: string, 318 | options?: ClearAnimationOptions, 319 | callback?: () => void 320 | ): void 321 | /** 322 | * 当从另一页面跳转到该页面时,获得与来源页面实例通信当事件通道,详见 [wx.navigateTo]((wx.navigateTo)) 323 | * 324 | * 最低基础库版本:[`2.7.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 325 | */ 326 | getOpenerEventChannel(): EventChannel 327 | /** 328 | * 绑定由 worklet 驱动的样式到相应的节点,详见 [worklet 动画](https://developers.weixin.qq.com/miniprogram/dev/framework/runtime/skyline/worklet.html) 329 | * 330 | * 最低基础库版本:[`2.29.0`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 331 | */ 332 | applyAnimatedStyle( 333 | selector: string, 334 | updater: () => Record, 335 | userConfig?: { immediate: boolean, flush: 'sync' | 'async' }, 336 | callback?: (res: { styleId: number }) => void 337 | ): void 338 | /** 339 | * 清除节点上 worklet 驱动样式的绑定关系 340 | * 341 | * 最低基础库版本:[`2.30.1`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 342 | */ 343 | clearAnimatedStyle( 344 | selector: string, 345 | styleIds: number[], 346 | callback?: () => void 347 | ): void 348 | /** 349 | * 获取更新性能统计信息,详见 [获取更新性能统计信息]((custom-component/update-perf-stat)) 350 | * 351 | * 352 | * 最低基础库版本:[`2.12.0`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 353 | */ 354 | setUpdatePerformanceListener( 355 | options: SetUpdatePerformanceListenerOption, 356 | callback?: UpdatePerformanceListener 357 | ): void 358 | 359 | /** 360 | * 在运行时获取页面或组件所在页面 `touch` 相关事件的 passive 配置,详见 [enablePassiveEvent]((configuration/app#enablePassiveEvent)) 361 | * 362 | * 最低基础库版本:[`2.25.1`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 363 | */ 364 | getPassiveEvent(callback: (config: PassiveConfig) => void): void 365 | /** 366 | * 在运行时切换页面或组件所在页面 `touch` 相关事件的 passive 配置,详见 [enablePassiveEvent]((configuration/app#enablePassiveEvent)) 367 | * 368 | * 最低基础库版本:[`2.25.1`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 369 | */ 370 | setPassiveEvent(config: PassiveConfig): void 371 | /** 372 | * 设置初始渲染缓存的动态数据 373 | * 374 | * 最低基础库版本:[`2.11.1`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 375 | */ 376 | setInitialRenderingCache(dynamicData: IAnyObject | null): void 377 | /** 378 | * 返回当前页面的 appBar 组件实例 379 | * 380 | * 最低基础库版本:[`3.3.1`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 381 | */ 382 | getAppBar(): TrivialInstance 383 | } 384 | 385 | interface ComponentOptions { 386 | /** 387 | * [启用多slot支持](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/wxml-wxss.html#组件wxml的slot) 388 | */ 389 | multipleSlots?: boolean 390 | /** 391 | * [组件样式隔离](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/wxml-wxss.html#组件样式隔离) 392 | */ 393 | addGlobalClass?: boolean 394 | /** 395 | * [组件样式隔离](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/wxml-wxss.html#组件样式隔离) 396 | */ 397 | styleIsolation?: 398 | | 'isolated' 399 | | 'apply-shared' 400 | | 'shared' 401 | | 'page-isolated' 402 | | 'page-apply-shared' 403 | | 'page-shared' 404 | /** 405 | * [纯数据字段](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/pure-data.html) 是一些不用于界面渲染的 data 字段,可以用于提升页面更新性能。从小程序基础库版本 2.8.2 开始支持。 406 | */ 407 | pureDataPattern?: RegExp 408 | /** 409 | * [虚拟化组件节点](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/wxml-wxss.html#%E8%99%9A%E6%8B%9F%E5%8C%96%E7%BB%84%E4%BB%B6%E8%8A%82%E7%82%B9) 使自定义组件内部的第一层节点由自定义组件本身完全决定。从小程序基础库版本 [`2.11.2`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 开始支持 */ 410 | virtualHost?: boolean 411 | } 412 | 413 | interface TriggerEventOption { 414 | /** 事件是否冒泡 415 | * 416 | * 默认值: `false` 417 | */ 418 | bubbles?: boolean 419 | /** 事件是否可以穿越组件边界,为false时,事件将只能在引用组件的节点树上触发,不进入其他任何组件内部 420 | * 421 | * 默认值: `false` 422 | */ 423 | composed?: boolean 424 | /** 事件是否拥有捕获阶段 425 | * 426 | * 默认值: `false` 427 | */ 428 | capturePhase?: boolean 429 | } 430 | 431 | interface RelationOption { 432 | /** 目标组件的相对关系 */ 433 | type: 'parent' | 'child' | 'ancestor' | 'descendant' 434 | /** 关系生命周期函数,当关系被建立在页面节点树中时触发,触发时机在组件attached生命周期之后 */ 435 | linked?(target: TrivialInstance): void 436 | /** 关系生命周期函数,当关系在页面节点树中发生改变时触发,触发时机在组件moved生命周期之后 */ 437 | linkChanged?(target: TrivialInstance): void 438 | /** 关系生命周期函数,当关系脱离页面节点树时触发,触发时机在组件detached生命周期之后 */ 439 | unlinked?(target: TrivialInstance): void 440 | /** 如果这一项被设置,则它表示关联的目标节点所应具有的behavior,所有拥有这一behavior的组件节点都会被关联 */ 441 | target?: string 442 | } 443 | 444 | interface PageLifetimes { 445 | /** 页面生命周期回调—监听页面显示 446 | * 447 | * 页面显示/切入前台时触发。 448 | */ 449 | show(): void 450 | /** 页面生命周期回调—监听页面隐藏 451 | * 452 | * 页面隐藏/切入后台时触发。 如 `navigateTo` 或底部 `tab` 切换到其他页面,小程序切入后台等。 453 | */ 454 | hide(): void 455 | /** 页面生命周期回调—监听页面尺寸变化 456 | * 457 | * 所在页面尺寸变化时执行 458 | */ 459 | resize(size: Page.IResizeOption): void 460 | /** 页面生命周期回调—监听页面路由动画完成 461 | * 462 | * 所在页面路由动画完成时执行 463 | */ 464 | routeDone(): void 465 | } 466 | 467 | type DefinitionFilter = ( 468 | /** 使用该 behavior 的 component/behavior 的定义对象 */ 469 | defFields: T, 470 | /** 该 behavior 所使用的 behavior 的 definitionFilter 函数列表 */ 471 | definitionFilterArr?: DefinitionFilter[] 472 | ) => void 473 | 474 | interface Lifetimes { 475 | /** 组件生命周期声明对象,组件的生命周期:`created`、`attached`、`ready`、`moved`、`detached` 将收归到 `lifetimes` 字段内进行声明,原有声明方式仍旧有效,如同时存在两种声明方式,则 `lifetimes` 字段内声明方式优先级最高 476 | * 477 | * 最低基础库: `2.2.3` */ 478 | lifetimes: Partial<{ 479 | /** 480 | * 在组件实例刚刚被创建时执行,注意此时不能调用 `setData` 481 | * 482 | * 最低基础库版本:[`1.6.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 483 | */ 484 | created(): void 485 | /** 486 | * 在组件实例进入页面节点树时执行 487 | * 488 | * 最低基础库版本:[`1.6.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 489 | */ 490 | attached(): void 491 | /** 492 | * 在组件在视图层布局完成后执行 493 | * 494 | * 最低基础库版本:[`1.6.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 495 | */ 496 | ready(): void 497 | /** 498 | * 在组件实例被移动到节点树另一个位置时执行 499 | * 500 | * 最低基础库版本:[`1.6.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 501 | */ 502 | moved(): void 503 | /** 504 | * 在组件实例被从页面节点树移除时执行 505 | * 506 | * 最低基础库版本:[`1.6.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 507 | */ 508 | detached(): void 509 | /** 510 | * 每当组件方法抛出错误时执行 511 | * 512 | * 最低基础库版本:[`2.4.1`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 513 | */ 514 | error(err: Error): void 515 | }> 516 | /** 517 | * @deprecated 旧式的定义方式,基础库 `2.2.3` 起请在 lifetimes 中定义 518 | * 519 | * 在组件实例刚刚被创建时执行 520 | * 521 | * 最低基础库版本:[`1.6.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 522 | */ 523 | created(): void 524 | /** 525 | * @deprecated 旧式的定义方式,基础库 `2.2.3` 起请在 lifetimes 中定义 526 | * 527 | * 在组件实例进入页面节点树时执行 528 | * 529 | * 最低基础库版本:[`1.6.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 530 | */ 531 | attached(): void 532 | /** 533 | * @deprecated 旧式的定义方式,基础库 `2.2.3` 起请在 lifetimes 中定义 534 | * 535 | * 在组件在视图层布局完成后执行 536 | * 537 | * 最低基础库版本:[`1.6.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 538 | */ 539 | ready(): void 540 | /** 541 | * @deprecated 旧式的定义方式,基础库 `2.2.3` 起请在 lifetimes 中定义 542 | * 543 | * 在组件实例被移动到节点树另一个位置时执行 544 | * 545 | * 最低基础库版本:[`1.6.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 546 | */ 547 | moved(): void 548 | /** 549 | * @deprecated 旧式的定义方式,基础库 `2.2.3` 起请在 lifetimes 中定义 550 | * 551 | * 在组件实例被从页面节点树移除时执行 552 | * 553 | * 最低基础库版本:[`1.6.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 554 | */ 555 | detached(): void 556 | /** 557 | * @deprecated 旧式的定义方式,基础库 `2.2.3` 起请在 lifetimes 中定义 558 | * 559 | * 每当组件方法抛出错误时执行 560 | * 561 | * 最低基础库版本:[`2.4.1`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 562 | */ 563 | error(err: Error): void 564 | } 565 | 566 | interface OtherOption { 567 | /** 568 | * 组件数据字段监听器,用于监听 properties 和 data 的变化,参见 [数据监听器](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/observer.html) 569 | * 570 | * 最低基础库版本:[`2.6.1`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 571 | */ 572 | observers: Record any> 573 | /** 组件间关系定义,参见 [组件间关系](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/lifetimes.html) */ 574 | relations: { 575 | [componentName: string]: RelationOption 576 | } 577 | /** 组件接受的外部样式类,参见 [外部样式类](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/wxml-wxss.html) */ 578 | externalClasses?: string[] 579 | /** 组件所在页面的生命周期声明对象,参见 [组件生命周期](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/lifetimes.html) 580 | * 581 | * 最低基础库版本: [`2.2.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) */ 582 | pageLifetimes?: Partial 583 | /** 一些选项(文档中介绍相关特性时会涉及具体的选项设置,这里暂不列举) */ 584 | options: ComponentOptions 585 | 586 | /** 定义段过滤器,用于自定义组件扩展,参见 [自定义组件扩展](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/extend.html) 587 | * 588 | * 最低基础库版本: [`2.2.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) */ 589 | definitionFilter?: DefinitionFilter 590 | /** 591 | * 组件自定义导出,当使用 `behavior: wx://component-export` 时,这个定义段可以用于指定组件被 selectComponent 调用时的返回值,参见 [组件间通信与事件](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/events.html) 592 | * 最低基础库版本: [`2.2.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) */ 593 | export: () => IAnyObject 594 | } 595 | 596 | interface KeyFrame { 597 | /** 关键帧的偏移,范围[0-1] */ 598 | offset?: number 599 | /** 动画缓动函数 */ 600 | ease?: string 601 | /** 基点位置,即 CSS transform-origin */ 602 | transformOrigin?: string 603 | /** 背景颜色,即 CSS background-color */ 604 | backgroundColor?: string 605 | /** 底边位置,即 CSS bottom */ 606 | bottom?: number | string 607 | /** 高度,即 CSS height */ 608 | height?: number | string 609 | /** 左边位置,即 CSS left */ 610 | left?: number | string 611 | /** 宽度,即 CSS width */ 612 | width?: number | string 613 | /** 不透明度,即 CSS opacity */ 614 | opacity?: number | string 615 | /** 右边位置,即 CSS right */ 616 | right?: number | string 617 | /** 顶边位置,即 CSS top */ 618 | top?: number | string 619 | /** 变换矩阵,即 CSS transform matrix */ 620 | matrix?: number[] 621 | /** 三维变换矩阵,即 CSS transform matrix3d */ 622 | matrix3d?: number[] 623 | /** 旋转,即 CSS transform rotate */ 624 | rotate?: number 625 | /** 三维旋转,即 CSS transform rotate3d */ 626 | rotate3d?: number[] 627 | /** X 方向旋转,即 CSS transform rotateX */ 628 | rotateX?: number 629 | /** Y 方向旋转,即 CSS transform rotateY */ 630 | rotateY?: number 631 | /** Z 方向旋转,即 CSS transform rotateZ */ 632 | rotateZ?: number 633 | /** 缩放,即 CSS transform scale */ 634 | scale?: number[] 635 | /** 三维缩放,即 CSS transform scale3d */ 636 | scale3d?: number[] 637 | /** X 方向缩放,即 CSS transform scaleX */ 638 | scaleX?: number 639 | /** Y 方向缩放,即 CSS transform scaleY */ 640 | scaleY?: number 641 | /** Z 方向缩放,即 CSS transform scaleZ */ 642 | scaleZ?: number 643 | /** 倾斜,即 CSS transform skew */ 644 | skew?: number[] 645 | /** X 方向倾斜,即 CSS transform skewX */ 646 | skewX?: number 647 | /** Y 方向倾斜,即 CSS transform skewY */ 648 | skewY?: number 649 | /** 位移,即 CSS transform translate */ 650 | translate?: Array 651 | /** 三维位移,即 CSS transform translate3d */ 652 | translate3d?: Array 653 | /** X 方向位移,即 CSS transform translateX */ 654 | translateX?: number | string 655 | /** Y 方向位移,即 CSS transform translateY */ 656 | translateY?: number | string 657 | /** Z 方向位移,即 CSS transform translateZ */ 658 | translateZ?: number | string 659 | } 660 | interface ClearAnimationOptions { 661 | /** 基点位置,即 CSS transform-origin */ 662 | transformOrigin?: boolean 663 | /** 背景颜色,即 CSS background-color */ 664 | backgroundColor?: boolean 665 | /** 底边位置,即 CSS bottom */ 666 | bottom?: boolean 667 | /** 高度,即 CSS height */ 668 | height?: boolean 669 | /** 左边位置,即 CSS left */ 670 | left?: boolean 671 | /** 宽度,即 CSS width */ 672 | width?: boolean 673 | /** 不透明度,即 CSS opacity */ 674 | opacity?: boolean 675 | /** 右边位置,即 CSS right */ 676 | right?: boolean 677 | /** 顶边位置,即 CSS top */ 678 | top?: boolean 679 | /** 变换矩阵,即 CSS transform matrix */ 680 | matrix?: boolean 681 | /** 三维变换矩阵,即 CSS transform matrix3d */ 682 | matrix3d?: boolean 683 | /** 旋转,即 CSS transform rotate */ 684 | rotate?: boolean 685 | /** 三维旋转,即 CSS transform rotate3d */ 686 | rotate3d?: boolean 687 | /** X 方向旋转,即 CSS transform rotateX */ 688 | rotateX?: boolean 689 | /** Y 方向旋转,即 CSS transform rotateY */ 690 | rotateY?: boolean 691 | /** Z 方向旋转,即 CSS transform rotateZ */ 692 | rotateZ?: boolean 693 | /** 缩放,即 CSS transform scale */ 694 | scale?: boolean 695 | /** 三维缩放,即 CSS transform scale3d */ 696 | scale3d?: boolean 697 | /** X 方向缩放,即 CSS transform scaleX */ 698 | scaleX?: boolean 699 | /** Y 方向缩放,即 CSS transform scaleY */ 700 | scaleY?: boolean 701 | /** Z 方向缩放,即 CSS transform scaleZ */ 702 | scaleZ?: boolean 703 | /** 倾斜,即 CSS transform skew */ 704 | skew?: boolean 705 | /** X 方向倾斜,即 CSS transform skewX */ 706 | skewX?: boolean 707 | /** Y 方向倾斜,即 CSS transform skewY */ 708 | skewY?: boolean 709 | /** 位移,即 CSS transform translate */ 710 | translate?: boolean 711 | /** 三维位移,即 CSS transform translate3d */ 712 | translate3d?: boolean 713 | /** X 方向位移,即 CSS transform translateX */ 714 | translateX?: boolean 715 | /** Y 方向位移,即 CSS transform translateY */ 716 | translateY?: boolean 717 | /** Z 方向位移,即 CSS transform translateZ */ 718 | translateZ?: boolean 719 | } 720 | interface ScrollTimelineKeyframe { 721 | composite?: 'replace' | 'add' | 'accumulate' | 'auto' 722 | easing?: string 723 | offset?: number | null 724 | [property: string]: string | number | null | undefined 725 | } 726 | interface ScrollTimelineOption { 727 | /** 指定滚动元素的选择器(只支持 scroll-view),该元素滚动时会驱动动画的进度 */ 728 | scrollSource: string 729 | /** 指定滚动的方向。有效值为 horizontal 或 vertical */ 730 | orientation?: string 731 | /** 指定开始驱动动画进度的滚动偏移量,单位 px */ 732 | startScrollOffset: number 733 | /** 指定停止驱动动画进度的滚动偏移量,单位 px */ 734 | endScrollOffset: number 735 | /** 起始和结束的滚动范围映射的时间长度,该时间可用于与关键帧动画里的时间 (duration) 相匹配,单位 ms */ 736 | timeRange: number 737 | } 738 | 739 | interface SetUpdatePerformanceListenerOption { 740 | /** 是否返回变更的 data 字段信息 */ 741 | withDataPaths?: WithDataPath 742 | } 743 | interface UpdatePerformanceListener { 744 | (res: UpdatePerformance): void 745 | } 746 | interface UpdatePerformance { 747 | /** 此次更新过程的 ID */ 748 | updateProcessId: number 749 | /** 对于子更新,返回它所属的更新过程 ID */ 750 | parentUpdateProcessId?: number 751 | /** 是否是被合并更新,如果是,则 updateProcessId 表示被合并到的更新过程 ID */ 752 | isMergedUpdate: boolean 753 | /** 此次更新的 data 字段信息,只有 withDataPaths 设为 true 时才会返回 */ 754 | dataPaths: WithDataPath extends true ? string[] : undefined 755 | /** 此次更新进入等待队列时的时间戳 */ 756 | pendingStartTimestamp: number 757 | /** 更新运算开始时的时间戳 */ 758 | updateStartTimestamp: number 759 | /** 更新运算结束时的时间戳 */ 760 | updateEndTimestamp: number 761 | } 762 | 763 | type PassiveConfig = 764 | | { 765 | /** 是否设置 touchmove 事件为 passive,默认为 `false` */ 766 | touchmove?: boolean 767 | /** 是否设置 touchstart 事件为 passive,默认为 `false` */ 768 | touchstart?: boolean 769 | /** 是否设置 wheel 事件为 passive,默认为 `false` */ 770 | wheel?: boolean 771 | } 772 | | boolean 773 | } 774 | /** Component构造器可用于定义组件,调用Component构造器时可以指定组件的属性、数据、方法等。 775 | * 776 | * * 使用 `this.data` 可以获取内部数据和属性值,但不要直接修改它们,应使用 `setData` 修改。 777 | * * 生命周期函数无法在组件方法中通过 `this` 访问到。 778 | * * 属性名应避免以 data 开头,即不要命名成 `dataXyz` 这样的形式,因为在 WXML 中, `data-xyz=""` 会被作为节点 dataset 来处理,而不是组件属性。 779 | * * 在一个组件的定义和使用时,组件的属性名和 data 字段相互间都不能冲突(尽管它们位于不同的定义段中)。 780 | * * 从基础库 `2.0.9` 开始,对象类型的属性和 data 字段中可以包含函数类型的子字段,即可以通过对象类型的属性字段来传递函数。低于这一版本的基础库不支持这一特性。 781 | * * `bug` : 对于 type 为 Object 或 Array 的属性,如果通过该组件自身的 `this.setData` 来改变属性值的一个子字段,则依旧会触发属性 observer ,且 observer 接收到的 `newVal` 是变化的那个子字段的值, `oldVal` 为空, `changedPath` 包含子字段的字段名相关信息。 782 | */ 783 | declare let Component: WechatMiniprogram.Component.Constructor 784 | -------------------------------------------------------------------------------- /types/wx/lib.wx.page.d.ts: -------------------------------------------------------------------------------- 1 | /*! ***************************************************************************** 2 | Copyright (c) 2025 Tencent, Inc. All rights reserved. 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | this software and associated documentation files (the "Software"), to deal in 6 | the Software without restriction, including without limitation the rights to 7 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 8 | of the Software, and to permit persons to whom the Software is furnished to do 9 | so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | SOFTWARE. 21 | ***************************************************************************** */ 22 | 23 | declare namespace WechatMiniprogram.Page { 24 | type Instance< 25 | TData extends DataOption, 26 | TCustom extends CustomOption 27 | > = OptionalInterface & 28 | InstanceProperties & 29 | InstanceMethods & 30 | Data & 31 | TCustom 32 | type Options< 33 | TData extends DataOption, 34 | TCustom extends CustomOption 35 | > = (TCustom & 36 | Partial> & 37 | Partial & { 38 | options?: Component.ComponentOptions 39 | }) & 40 | ThisType> 41 | type TrivialInstance = Instance 42 | interface Constructor { 43 | ( 44 | options: Options 45 | ): void 46 | } 47 | interface ILifetime { 48 | /** 生命周期回调—监听页面加载 49 | * 50 | * 页面加载时触发。一个页面只会调用一次,可以在 onLoad 的参数中获取打开当前页面路径中的参数。 51 | */ 52 | onLoad( 53 | /** 打开当前页面路径中的参数 */ 54 | query: Record 55 | ): void | Promise 56 | /** 生命周期回调—监听页面显示 57 | * 58 | * 页面显示/切入前台时触发。 59 | */ 60 | onShow(): void | Promise 61 | /** 生命周期回调—监听页面初次渲染完成 62 | * 63 | * 页面初次渲染完成时触发。一个页面只会调用一次,代表页面已经准备妥当,可以和视图层进行交互。 64 | * 65 | 66 | * 注意:对界面内容进行设置的 API 如`wx.setNavigationBarTitle`,请在`onReady`之后进行。 67 | */ 68 | onReady(): void | Promise 69 | /** 生命周期回调—监听页面隐藏 70 | * 71 | * 页面隐藏/切入后台时触发。 如 `navigateTo` 或底部 `tab` 切换到其他页面,小程序切入后台等。 72 | */ 73 | onHide(): void | Promise 74 | /** 生命周期回调—监听页面卸载 75 | * 76 | * 页面卸载时触发。如`redirectTo`或`navigateBack`到其他页面时。 77 | */ 78 | onUnload(): void | Promise 79 | /** 生命周期回调—监听路由动画完成 80 | * 81 | * 路由动画完成时触发。如 wx.navigateTo 页面完全推入后 或 wx.navigateBack 页面完全恢复时。 82 | */ 83 | onRouteDone(): void | Promise 84 | /** 监听用户下拉动作 85 | * 86 | * 监听用户下拉刷新事件。 87 | * - 需要在`app.json`的`window`选项中或页面配置中开启`enablePullDownRefresh`。 88 | * - 可以通过`wx.startPullDownRefresh`触发下拉刷新,调用后触发下拉刷新动画,效果与用户手动下拉刷新一致。 89 | * - 当处理完数据刷新后,`wx.stopPullDownRefresh`可以停止当前页面的下拉刷新。 90 | */ 91 | onPullDownRefresh(): void | Promise 92 | /** 页面上拉触底事件的处理函数 93 | * 94 | * 监听用户上拉触底事件。 95 | * - 可以在`app.json`的`window`选项中或页面配置中设置触发距离`onReachBottomDistance`。 96 | * - 在触发距离内滑动期间,本事件只会被触发一次。 97 | */ 98 | onReachBottom(): void | Promise 99 | /** 用户点击右上角转发 100 | * 101 | * 监听用户点击页面内转发按钮(`