Type definitions for APIs of WeChat Mini Program in TypeScript
npx @tessl/cli install tessl/npm-miniprogram-api-typings@4.1.0WeChat 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.
npm install miniprogram-api-typingsThis library provides global type definitions that are automatically available without explicit imports when properly configured:
TypeScript Configuration:
{
"compilerOptions": {
"types": ["miniprogram-api-typings"]
}
}Global APIs Available:
// Global objects are automatically typed when this library is included
wx.showToast({ title: 'Hello WeChat!' });
console.log('WeChat Mini Program');
// Global constructors are available
App({
onLaunch() {
console.log('App launched');
}
});
Page({
data: {
message: 'Hello World'
},
onLoad() {
this.setData({ message: 'Page loaded' });
}
});
Component({
properties: {
text: String
},
methods: {
handleTap() {
this.triggerEvent('tap');
}
}
});
// WebGL context creation
const canvas = wx.createOffscreenCanvas();
const gl = canvas.getContext('webgl2');
// Cloud services
wx.cloud.callFunction({ name: 'getData' });// App definition with lifecycle methods
App({
globalData: {
userInfo: null
},
onLaunch(options) {
// App initialization
console.log('App launched with scene:', options.scene);
},
onShow(options) {
// App foreground
console.log('App showed');
}
});
// Page definition with data and methods
Page({
data: {
items: [] as string[],
loading: false
},
onLoad(query) {
// Page initialization
this.loadData();
},
loadData() {
this.setData({ loading: true });
wx.request({
url: 'https://api.example.com/data',
success: (res) => {
this.setData({
items: res.data,
loading: false
});
}
});
}
});
// Component definition with properties and methods
Component({
properties: {
title: String,
count: {
type: Number,
value: 0
}
},
data: {
visible: true
},
methods: {
handleClick() {
this.triggerEvent('itemClick', {
title: this.properties.title,
count: this.properties.count
});
}
}
});The WeChat Mini Program API Typings library is organized around several key components:
wx, console) providing platform APIsApp, Page, Component, Behavior) for defining application structureWechatMiniprogram namespaceComplete type definitions for WeChat Mini Program app and page lifecycles, including launch options, scene values, and lifecycle methods.
// App constructor
function App<TData extends WechatMiniprogram.App.DataOption>(
options: WechatMiniprogram.App.Option<TData>
): void;
// Page constructor
function Page<TData extends WechatMiniprogram.Page.DataOption>(
options: WechatMiniprogram.Page.Option<TData>
): void;
// Get current app instance
function getApp<T extends WechatMiniprogram.App.Instance>(
options?: WechatMiniprogram.App.GetAppOption
): T;
// Get current pages stack
function getCurrentPages<T extends WechatMiniprogram.Page.Instance[]>(): T;Comprehensive component system with properties, methods, lifecycle events, and component relationships.
// Component constructor
function Component<
TData extends WechatMiniprogram.Component.DataOption,
TProperty extends WechatMiniprogram.Component.PropertyOption,
TMethod extends WechatMiniprogram.Component.MethodOption
>(
options: WechatMiniprogram.Component.Option<TData, TProperty, TMethod>
): void;
// Behavior constructor for component mixins
function Behavior<
TData extends WechatMiniprogram.Behavior.DataOption,
TProperty extends WechatMiniprogram.Behavior.PropertyOption,
TMethod extends WechatMiniprogram.Behavior.MethodOption
>(
options: WechatMiniprogram.Behavior.Option<TData, TProperty, TMethod>
): void;Main WeChat platform APIs including network requests, storage, UI interactions, media operations, device access, and system information.
declare const wx: WechatMiniprogram.Wx;
// Network APIs
interface Wx {
request(options: RequestOption): RequestTask;
uploadFile(options: UploadFileOption): UploadTask;
downloadFile(options: DownloadFileOption): DownloadTask;
}
// Storage APIs
interface Wx {
setStorage(options: SetStorageOption): void;
getStorage(options: GetStorageOption): void;
removeStorage(options: RemoveStorageOption): void;
clearStorage(options?: ClearStorageOption): void;
}
// UI APIs
interface Wx {
showToast(options: ShowToastOption): void;
showModal(options: ShowModalOption): void;
showLoading(options: ShowLoadingOption): void;
navigateTo(options: NavigateToOption): void;
}Complete cloud function, database, and storage APIs for WeChat Mini Program cloud development.
// Cloud namespace
declare const wx: {
cloud: WxCloud;
};
interface WxCloud {
callFunction(options: ICloud.CallFunctionOptions): Promise<ICloud.CallFunctionResult>;
database(options?: ICloud.DatabaseOptions): DB.Database;
uploadFile(options: ICloud.UploadFileOptions): Promise<ICloud.UploadFileResult>;
downloadFile(options: ICloud.DownloadFileOptions): Promise<ICloud.DownloadFileResult>;
}2D Canvas and WebGL rendering contexts for graphics and game development.
// Canvas context creation
interface Wx {
createCanvasContext(canvasId: string, componentInstance?: any): CanvasRenderingContext2D;
}
// 2D Canvas context with complete drawing API
interface CanvasRenderingContext2D {
fillRect(x: number, y: number, width: number, height: number): void;
strokeRect(x: number, y: number, width: number, height: number): void;
fillText(text: string, x: number, y: number, maxWidth?: number): void;
drawImage(image: any, dx: number, dy: number): void;
}Comprehensive event type definitions for touch interactions, UI components, media events, and custom events.
// Touch event types
interface TouchEvent {
type: string;
timeStamp: number;
target: EventTarget;
currentTarget: EventTarget;
touches: Touch[];
changedTouches: Touch[];
}
// Component event types
interface ButtonTapEvent {
type: 'tap';
target: EventTarget;
currentTarget: EventTarget;
detail: {};
}Comprehensive payment system for WeChat Mini Programs, including WeChat Pay, global payments, plugin payments, and merchant transfers.
// WeChat Payment
function requestPayment<T extends RequestPaymentOption = RequestPaymentOption>(
option: T
): PromisifySuccessResult<T, RequestPaymentOption>;
// Global Payment System
function createGlobalPayment(option: CreateGlobalPaymentOption): GlobalPayment;
// Common Payment Interface
function requestCommonPayment(option: RequestCommonPaymentOption): void;
// Plugin Payment
function requestPluginPayment(option: RequestPluginPaymentOption): void;
// Merchant Transfer
function requestMerchantTransfer(option: RequestMerchantTransferOption): void;Comprehensive Bluetooth Low Energy (BLE) and Near Field Communication (NFC) APIs for IoT device integration and smart device connectivity.
// Bluetooth Adapter Management
function openBluetoothAdapter<T extends OpenBluetoothAdapterOption = OpenBluetoothAdapterOption>(
option?: T
): PromisifySuccessResult<T, OpenBluetoothAdapterOption>;
function closeBluetoothAdapter<T extends CloseBluetoothAdapterOption = CloseBluetoothAdapterOption>(
option?: T
): PromisifySuccessResult<T, CloseBluetoothAdapterOption>;
// BLE Connection Management
function createBLEConnection<T extends CreateBLEConnectionOption = CreateBLEConnectionOption>(
option: T
): PromisifySuccessResult<T, CreateBLEConnectionOption>;
// NFC Support
function getNFCAdapter(): NFCAdapter;Comprehensive AI and Machine Learning capabilities including local ONNX model inference, cloud AI services, and AI-powered features.
// Local AI Inference
function createInferenceSession(option: CreateInferenceSessionOption): InferenceSession;
function getInferenceEnvInfo(option?: GetInferenceEnvInfoOption): void;
// Face Detection
function initFaceDetect(option: InitFaceDetectOption): void;
function faceDetect(option: FaceDetectOption): void;
// Vision Kit Session
function createVKSession(option: CreateVKSessionOption): VKSession;Comprehensive device and hardware integration APIs including sensors, biometric authentication, device information, and system interactions.
// Biometric Authentication
function checkIsSupportSoterAuthentication(option: CheckIsSupportSoterAuthenticationOption): void;
function startSoterAuthentication(option: StartSoterAuthenticationOption): void;
// Device Sensors
function startAccelerometer(option?: StartAccelerometerOption): void;
function startGyroscope(option?: StartGyroscopeOption): void;
function startCompass(option?: StartCompassOption): void;
// System Information
function getSystemInfo(option?: GetSystemInfoOption): void;
function getSystemInfoSync(): SystemInfo;// Primary WeChat API object
declare const wx: WechatMiniprogram.Wx;
// Console for logging
declare const console: WechatMiniprogram.Console;
// Module system
declare const require: Require;
declare const requirePlugin: RequirePlugin;
declare function requireMiniProgram(): any;
// Timer functions
declare function setTimeout(
callback: (...args: any[]) => any,
delay?: number,
...rest: any[]
): number;
declare function setInterval(
callback: (...args: any[]) => any,
delay?: number,
...rest: any[]
): number;
declare function clearTimeout(timeoutID: number): void;
declare function clearInterval(intervalID: number): void;// Root namespace
declare namespace WechatMiniprogram {
// Utility types
type IAnyObject = Record<string, any>;
type Optional<F> = F extends (arg: infer P) => infer R ? (arg?: P) => R : F;
type OptionalInterface<T> = { [K in keyof T]: Optional<T[K]> };
// Promise utility for async methods
interface AsyncMethodOptionLike {
success?: (...args: any[]) => void;
}
type PromisifySuccessResult<
P,
T extends AsyncMethodOptionLike
> = P extends { success: any }
? void
: P extends { fail: any }
? void
: P extends { complete: any }
? void
: Promise<Parameters<Exclude<T['success'], undefined>>[0]>;
}
// Require interface for module loading
interface Require {
(
module: string,
callback?: (moduleExport: any) => void,
errorCallback?: (err: any) => void
): any;
async(module: string): Promise<any>;
}
// Plugin require interface
interface RequirePlugin {
(
module: string,
callback?: (pluginExport: any) => void
): any;
async(module: string): Promise<any>;
}