参考微信小程序官方ts基础工程:
tsconfig.json
类型引入之后,就可以在我们的ts代码中,直接调用 wx 相关方法,编译期不会报错,运行期必须在微信环境下!
// wx 通过 *.d.ts 类型声明, 相当于定义了“全局对象”,类似 window 对象
/**
* 拨号
* @param phoneNumber
*/
export const makePhoneCall = function (phoneNumber: string) {
wx.makePhoneCall({
phoneNumber,
});
};
引入node_modules中的类型声明:
修改 typings/index.d.ts
/// <reference types="miniprogram-api-typings" />
// wx 通过 *.d.ts 类型声明, 相当于定义了“全局对象”,类似 window 对象
/**
* 拨号
* @param phoneNumber
*/
export const makePhoneCall = function (phoneNumber: string) {
wx.makePhoneCall({
phoneNumber,
});
};
需要安装miniprogram-api-typings依赖包:npm i miniprogram-api-typings -D
参考:
https://www.tslang.cn/docs/handbook/declaration-files/consumption.html
https://www.tslang.cn/docs/handbook/tsconfig-json.html