0
# WeChat Mini Program API Typings
1
2
WeChat Mini Program API Typings provides comprehensive TypeScript type definitions for WeChat Mini Program development. This library enables developers to build WeChat Mini Programs with full type safety, IntelliSense support, and compile-time error detection across the entire WeChat Mini Program ecosystem.
3
4
## Package Information
5
6
- **Package Name**: miniprogram-api-typings
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install miniprogram-api-typings`
10
11
## Core Imports
12
13
This library provides global type definitions that are automatically available without explicit imports when properly configured:
14
15
**TypeScript Configuration:**
16
```json
17
{
18
"compilerOptions": {
19
"types": ["miniprogram-api-typings"]
20
}
21
}
22
```
23
24
**Global APIs Available:**
25
```typescript
26
// Global objects are automatically typed when this library is included
27
wx.showToast({ title: 'Hello WeChat!' });
28
console.log('WeChat Mini Program');
29
30
// Global constructors are available
31
App({
32
onLaunch() {
33
console.log('App launched');
34
}
35
});
36
37
Page({
38
data: {
39
message: 'Hello World'
40
},
41
onLoad() {
42
this.setData({ message: 'Page loaded' });
43
}
44
});
45
46
Component({
47
properties: {
48
text: String
49
},
50
methods: {
51
handleTap() {
52
this.triggerEvent('tap');
53
}
54
}
55
});
56
57
// WebGL context creation
58
const canvas = wx.createOffscreenCanvas();
59
const gl = canvas.getContext('webgl2');
60
61
// Cloud services
62
wx.cloud.callFunction({ name: 'getData' });
63
```
64
65
## Basic Usage
66
67
```typescript
68
// App definition with lifecycle methods
69
App({
70
globalData: {
71
userInfo: null
72
},
73
onLaunch(options) {
74
// App initialization
75
console.log('App launched with scene:', options.scene);
76
},
77
onShow(options) {
78
// App foreground
79
console.log('App showed');
80
}
81
});
82
83
// Page definition with data and methods
84
Page({
85
data: {
86
items: [] as string[],
87
loading: false
88
},
89
onLoad(query) {
90
// Page initialization
91
this.loadData();
92
},
93
loadData() {
94
this.setData({ loading: true });
95
wx.request({
96
url: 'https://api.example.com/data',
97
success: (res) => {
98
this.setData({
99
items: res.data,
100
loading: false
101
});
102
}
103
});
104
}
105
});
106
107
// Component definition with properties and methods
108
Component({
109
properties: {
110
title: String,
111
count: {
112
type: Number,
113
value: 0
114
}
115
},
116
data: {
117
visible: true
118
},
119
methods: {
120
handleClick() {
121
this.triggerEvent('itemClick', {
122
title: this.properties.title,
123
count: this.properties.count
124
});
125
}
126
}
127
});
128
```
129
130
## Architecture
131
132
The WeChat Mini Program API Typings library is organized around several key components:
133
134
- **Global Objects**: Primary interfaces (`wx`, `console`) providing platform APIs
135
- **Constructors**: Framework constructors (`App`, `Page`, `Component`, `Behavior`) for defining application structure
136
- **Type System**: Comprehensive type definitions covering all WeChat Mini Program APIs
137
- **Event System**: Complete event type definitions for user interactions and system events
138
- **Namespace Organization**: Logical grouping of related functionality under the `WechatMiniprogram` namespace
139
140
## Capabilities
141
142
### App and Page Lifecycle
143
144
Complete type definitions for WeChat Mini Program app and page lifecycles, including launch options, scene values, and lifecycle methods.
145
146
```typescript { .api }
147
// App constructor
148
function App<TData extends WechatMiniprogram.App.DataOption>(
149
options: WechatMiniprogram.App.Option<TData>
150
): void;
151
152
// Page constructor
153
function Page<TData extends WechatMiniprogram.Page.DataOption>(
154
options: WechatMiniprogram.Page.Option<TData>
155
): void;
156
157
// Get current app instance
158
function getApp<T extends WechatMiniprogram.App.Instance>(
159
options?: WechatMiniprogram.App.GetAppOption
160
): T;
161
162
// Get current pages stack
163
function getCurrentPages<T extends WechatMiniprogram.Page.Instance[]>(): T;
164
```
165
166
[App and Page Lifecycle](./app-page-lifecycle.md)
167
168
### Component System
169
170
Comprehensive component system with properties, methods, lifecycle events, and component relationships.
171
172
```typescript { .api }
173
// Component constructor
174
function Component<
175
TData extends WechatMiniprogram.Component.DataOption,
176
TProperty extends WechatMiniprogram.Component.PropertyOption,
177
TMethod extends WechatMiniprogram.Component.MethodOption
178
>(
179
options: WechatMiniprogram.Component.Option<TData, TProperty, TMethod>
180
): void;
181
182
// Behavior constructor for component mixins
183
function Behavior<
184
TData extends WechatMiniprogram.Behavior.DataOption,
185
TProperty extends WechatMiniprogram.Behavior.PropertyOption,
186
TMethod extends WechatMiniprogram.Behavior.MethodOption
187
>(
188
options: WechatMiniprogram.Behavior.Option<TData, TProperty, TMethod>
189
): void;
190
```
191
192
[Component System](./component-system.md)
193
194
### Core WeChat APIs
195
196
Main WeChat platform APIs including network requests, storage, UI interactions, media operations, device access, and system information.
197
198
```typescript { .api }
199
declare const wx: WechatMiniprogram.Wx;
200
201
// Network APIs
202
interface Wx {
203
request(options: RequestOption): RequestTask;
204
uploadFile(options: UploadFileOption): UploadTask;
205
downloadFile(options: DownloadFileOption): DownloadTask;
206
}
207
208
// Storage APIs
209
interface Wx {
210
setStorage(options: SetStorageOption): void;
211
getStorage(options: GetStorageOption): void;
212
removeStorage(options: RemoveStorageOption): void;
213
clearStorage(options?: ClearStorageOption): void;
214
}
215
216
// UI APIs
217
interface Wx {
218
showToast(options: ShowToastOption): void;
219
showModal(options: ShowModalOption): void;
220
showLoading(options: ShowLoadingOption): void;
221
navigateTo(options: NavigateToOption): void;
222
}
223
```
224
225
[Core WeChat APIs](./core-apis.md)
226
227
### Cloud Services
228
229
Complete cloud function, database, and storage APIs for WeChat Mini Program cloud development.
230
231
```typescript { .api }
232
// Cloud namespace
233
declare const wx: {
234
cloud: WxCloud;
235
};
236
237
interface WxCloud {
238
callFunction(options: ICloud.CallFunctionOptions): Promise<ICloud.CallFunctionResult>;
239
database(options?: ICloud.DatabaseOptions): DB.Database;
240
uploadFile(options: ICloud.UploadFileOptions): Promise<ICloud.UploadFileResult>;
241
downloadFile(options: ICloud.DownloadFileOptions): Promise<ICloud.DownloadFileResult>;
242
}
243
```
244
245
[Cloud Services](./cloud-services.md)
246
247
### Canvas and Graphics
248
249
2D Canvas and WebGL rendering contexts for graphics and game development.
250
251
```typescript { .api }
252
// Canvas context creation
253
interface Wx {
254
createCanvasContext(canvasId: string, componentInstance?: any): CanvasRenderingContext2D;
255
}
256
257
// 2D Canvas context with complete drawing API
258
interface CanvasRenderingContext2D {
259
fillRect(x: number, y: number, width: number, height: number): void;
260
strokeRect(x: number, y: number, width: number, height: number): void;
261
fillText(text: string, x: number, y: number, maxWidth?: number): void;
262
drawImage(image: any, dx: number, dy: number): void;
263
}
264
```
265
266
[Canvas and Graphics](./canvas-graphics.md)
267
268
### Event System
269
270
Comprehensive event type definitions for touch interactions, UI components, media events, and custom events.
271
272
```typescript { .api }
273
// Touch event types
274
interface TouchEvent {
275
type: string;
276
timeStamp: number;
277
target: EventTarget;
278
currentTarget: EventTarget;
279
touches: Touch[];
280
changedTouches: Touch[];
281
}
282
283
// Component event types
284
interface ButtonTapEvent {
285
type: 'tap';
286
target: EventTarget;
287
currentTarget: EventTarget;
288
detail: {};
289
}
290
```
291
292
[Event System](./event-system.md)
293
294
### Payment APIs
295
296
Comprehensive payment system for WeChat Mini Programs, including WeChat Pay, global payments, plugin payments, and merchant transfers.
297
298
```typescript { .api }
299
// WeChat Payment
300
function requestPayment<T extends RequestPaymentOption = RequestPaymentOption>(
301
option: T
302
): PromisifySuccessResult<T, RequestPaymentOption>;
303
304
// Global Payment System
305
function createGlobalPayment(option: CreateGlobalPaymentOption): GlobalPayment;
306
307
// Common Payment Interface
308
function requestCommonPayment(option: RequestCommonPaymentOption): void;
309
310
// Plugin Payment
311
function requestPluginPayment(option: RequestPluginPaymentOption): void;
312
313
// Merchant Transfer
314
function requestMerchantTransfer(option: RequestMerchantTransferOption): void;
315
```
316
317
[Payment APIs](./payment-apis.md)
318
319
### Bluetooth & NFC APIs
320
321
Comprehensive Bluetooth Low Energy (BLE) and Near Field Communication (NFC) APIs for IoT device integration and smart device connectivity.
322
323
```typescript { .api }
324
// Bluetooth Adapter Management
325
function openBluetoothAdapter<T extends OpenBluetoothAdapterOption = OpenBluetoothAdapterOption>(
326
option?: T
327
): PromisifySuccessResult<T, OpenBluetoothAdapterOption>;
328
329
function closeBluetoothAdapter<T extends CloseBluetoothAdapterOption = CloseBluetoothAdapterOption>(
330
option?: T
331
): PromisifySuccessResult<T, CloseBluetoothAdapterOption>;
332
333
// BLE Connection Management
334
function createBLEConnection<T extends CreateBLEConnectionOption = CreateBLEConnectionOption>(
335
option: T
336
): PromisifySuccessResult<T, CreateBLEConnectionOption>;
337
338
// NFC Support
339
function getNFCAdapter(): NFCAdapter;
340
```
341
342
[Bluetooth & NFC APIs](./bluetooth-nfc-apis.md)
343
344
### AI & Machine Learning APIs
345
346
Comprehensive AI and Machine Learning capabilities including local ONNX model inference, cloud AI services, and AI-powered features.
347
348
```typescript { .api }
349
// Local AI Inference
350
function createInferenceSession(option: CreateInferenceSessionOption): InferenceSession;
351
352
function getInferenceEnvInfo(option?: GetInferenceEnvInfoOption): void;
353
354
// Face Detection
355
function initFaceDetect(option: InitFaceDetectOption): void;
356
357
function faceDetect(option: FaceDetectOption): void;
358
359
// Vision Kit Session
360
function createVKSession(option: CreateVKSessionOption): VKSession;
361
```
362
363
[AI & Machine Learning APIs](./ai-ml-apis.md)
364
365
### Device & Hardware APIs
366
367
Comprehensive device and hardware integration APIs including sensors, biometric authentication, device information, and system interactions.
368
369
```typescript { .api }
370
// Biometric Authentication
371
function checkIsSupportSoterAuthentication(option: CheckIsSupportSoterAuthenticationOption): void;
372
373
function startSoterAuthentication(option: StartSoterAuthenticationOption): void;
374
375
// Device Sensors
376
function startAccelerometer(option?: StartAccelerometerOption): void;
377
378
function startGyroscope(option?: StartGyroscopeOption): void;
379
380
function startCompass(option?: StartCompassOption): void;
381
382
// System Information
383
function getSystemInfo(option?: GetSystemInfoOption): void;
384
385
function getSystemInfoSync(): SystemInfo;
386
```
387
388
[Device & Hardware APIs](./device-hardware-apis.md)
389
390
## Global Objects
391
392
```typescript { .api }
393
// Primary WeChat API object
394
declare const wx: WechatMiniprogram.Wx;
395
396
// Console for logging
397
declare const console: WechatMiniprogram.Console;
398
399
// Module system
400
declare const require: Require;
401
declare const requirePlugin: RequirePlugin;
402
declare function requireMiniProgram(): any;
403
404
// Timer functions
405
declare function setTimeout(
406
callback: (...args: any[]) => any,
407
delay?: number,
408
...rest: any[]
409
): number;
410
411
declare function setInterval(
412
callback: (...args: any[]) => any,
413
delay?: number,
414
...rest: any[]
415
): number;
416
417
declare function clearTimeout(timeoutID: number): void;
418
declare function clearInterval(intervalID: number): void;
419
```
420
421
## Types
422
423
```typescript { .api }
424
// Root namespace
425
declare namespace WechatMiniprogram {
426
// Utility types
427
type IAnyObject = Record<string, any>;
428
type Optional<F> = F extends (arg: infer P) => infer R ? (arg?: P) => R : F;
429
type OptionalInterface<T> = { [K in keyof T]: Optional<T[K]> };
430
431
// Promise utility for async methods
432
interface AsyncMethodOptionLike {
433
success?: (...args: any[]) => void;
434
}
435
436
type PromisifySuccessResult<
437
P,
438
T extends AsyncMethodOptionLike
439
> = P extends { success: any }
440
? void
441
: P extends { fail: any }
442
? void
443
: P extends { complete: any }
444
? void
445
: Promise<Parameters<Exclude<T['success'], undefined>>[0]>;
446
}
447
448
// Require interface for module loading
449
interface Require {
450
(
451
module: string,
452
callback?: (moduleExport: any) => void,
453
errorCallback?: (err: any) => void
454
): any;
455
async(module: string): Promise<any>;
456
}
457
458
// Plugin require interface
459
interface RequirePlugin {
460
(
461
module: string,
462
callback?: (pluginExport: any) => void
463
): any;
464
async(module: string): Promise<any>;
465
}
466
```