typescript之typings(*.d.ts类型声明)

参考微信小程序官方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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值