wechat-miniprogram / api-typings Goto Github PK
View Code? Open in Web Editor NEWType definitions for APIs of Wechat Mini Program in TypeScript
License: MIT License
Type definitions for APIs of Wechat Mini Program in TypeScript
License: MIT License
看了下有定义 GetSystemInfoSyncResult 但没有使用
Is it a bug in demo ?
wx.removeStorage({
key: 'key',
success(res) {
console.log(res.data)
}
})
res.data
is undefined
in runtime.
https://developers.weixin.qq.com/miniprogram/dev/api/wx.removeStorage.html
在整个项目中找不到namespace WechatMiniprogram
component 定义不错,但是有些小BUG
api-typings/types/wx/lib.wx.component.d.ts
Line 176 in 4593cb0
这里的methods
参数不一定为空,应该是任意的
不是我说,compoenent的定义也太糊弄了吧?各种报错。可不可以用心做一份。
declare interface BaseComponent extends ComponentLifetimes {
/** 组件的对外属性,是属性名到属性设置的映射表 */
properties?: {
[propertyName: string]: PropertyOption;
};
/** 组件的内部数据,和 `properties` 一同用于组件的模板渲染 */
data?: object;
. ..
此处写法有问题,对于属性,官网是允许简写的。
Component({
properties: {
myProperty: { // 属性名
type: String,
value: ''
},
myProperty2: String // 简化的定义方式
},
...
另外,由于property在组件初始化的时候会变成data的引用,他们两个本质上是一个对象(详见官方文档),所以访问的时候会出现Property 'xx' does not exist on type 'object'.
修改建议:
declare interface BaseComponent extends ComponentLifetimes {
/** 组件的对外属性,是属性名到属性设置的映射表 */
properties?: {
[propertyName: string]: PropertyOption | Boolean | String | Number | Array | Object;
};
/** 组件的内部数据,和 `properties` 一同用于组件的模板渲染 */
data?: IAnyObject;
. ..
然后在访问属性的时候,不使用this.proproperties.XXX
而使用this.data.XXX as [type]
来规避问题
CanvasContext.draw
's params should be optional. The following usages should be all correct:
ctx.draw();
ctx.draw(true);
ctx.draw(true, () => {});
https://developers.weixin.qq.com/miniprogram/dev/api/CanvasContext.draw.html
这里应该 是 wx.SelectorQuery
和 wx.IntersectionObserver
?
很多api其实都存在这个问题,这个也基本上是一个文档的问题.....
我以wx.navigateBack这个API举例说明吧....
文档中说delta
是必填项,也就是说,如果我想要单纯做一个返回,我应该wx.navigateBack({delta:1)
。
但是返回上一页的逻辑是如此的常用,以至于这个api实际上从我所入坑的1.2.几版本,delta其实就是一个可选参数,或者说这个api本来就可以不传入对象,wx.navigateBack()
的调用结果一直都是返回上一页,根本不会报错....
所以我相信不止我一个开发者会大量的写wx.navigateBack()
这种调用形式.....而在TS里就会报错,我每次都是手动把delta:number
这类改成delta?:number
。
再举一种例子,同理类似的API还有wx.setTabBarStyle
,wx.setBackgroundColor
,setNavigationBarColor
这三货。
这三个API的参数很多都是必填,但是开发中会经常遇到就想改特定的某一项,这个时候只传这一项实际上是完全OK的,根本不需要把其他必填的参数都按照原来的再传一遍......
所以请求可不可以把实际上可以省略的这种通用api的调用也给改一下呢?
报错如下:
node_modules/miniprogram-api-typings/types/wx/lib.wx.cloud.d.ts(202,19): error TS2649: Cannot augment module 'wx' with value exports because it resolves to a non-module entity
版本:
^2.6.5-1
when will it be updated?
api-typings/types/wx/lib.wx.cloud.d.ts
Lines 861 to 897 in f98804c
这部分声明在TypeScript
的 lib.es5
的定义中已经包含这个了
https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es5.d.ts#L1421-L1489
小程序的定义应该是依赖ES5
(或与ES5共存的),
这部分不应该再次引入,否在会造成重复定义的问题
The document said CanvasContext.drawImage
has 3 override types.
But there is only the last one:
https://github.com/wechat-miniprogram/api-typings/blob/master/types/wx/lib.wx.api.d.ts#L5012-L5031
{
properties:{
key:Boolean // 直接指明类型
}
}
optionalTypes
为可选属性 ?BaseComponent
中声明了,不用再在继承的 WxComponent
重新定义了api-typings/types/wx/lib.wx.component.d.ts
Lines 68 to 73 in 4593cb0
api-typings/types/wx/lib.wx.component.d.ts
Lines 165 to 171 in 4593cb0
Should counterclockwise
in CanvasContext.arc
's params be boolean
instead of number
?
Since there is no demo in document I'm not sure which one is correct.
WeChat Mini Program document: https://developers.weixin.qq.com/miniprogram/dev/api/CanvasContext.arc.html
MDN: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/arc
I guess there is a bug here.
https://github.com/wechat-miniprogram/api-typings/blob/master/types/wx/lib.wx.api.d.ts#L1050-L1056
The second argument of wx.reportAnalytics
should be { [key: string]: any }
instead of { key: string, value: any }
.
Document: https://developers.weixin.qq.com/miniprogram/dev/api/wx.reportAnalytics.html
interface ChooseMessageFileSuccessCallbackResult {
/** 返回选择的文件的本地临时文件对象数组 */
tempFiles: ChooseFile;
}
interface ChooseMessageFileSuccessCallbackResult {
/** 返回选择的文件的本地临时文件对象数组 */
tempFiles: ChooseFile[];
}
babel 7 支持 typescript 了,但是不支持 namespace,能否像 Component 那样移除 namespace ?
interface OpenCardOption {
/** 需要打开的卡券列表 */
cardList: OpenCardRequestInfo;
/** 接口调用结束的回调函数(调用成功、失败都会执行) */
complete?: OpenCardCompleteCallback;
/** 接口调用失败的回调函数 */
fail?: OpenCardFailCallback;
/** 接口调用成功的回调函数 */
success?: OpenCardSuccessCallback;
}
应为
interface OpenCardOption {
/** 需要打开的卡券列表 */
cardList: OpenCardRequestInfo[];
/** 接口调用结束的回调函数(调用成功、失败都会执行) */
complete?: OpenCardCompleteCallback;
/** 接口调用失败的回调函数 */
fail?: OpenCardFailCallback;
/** 接口调用成功的回调函数 */
success?: OpenCardSuccessCallback;
}
使用了最新的2.7.7-1,lib.wx.api.d里面的wx不见了,这样搞还能用吗,没有类型提示了。
但是接口定义只有一个errMsg,
interface GeneralCallbackResult {
errMsg: string;
}
失败还有errCode
应该是文档表达的不好。
在案例中:
wx.reportAnalytics('purchase', {
price: 120,
color: 'red'
})
很明显,data应该是如下的的形式:
/** 上报的自定义数据。 */
interface Data {
/** 配置中的字段名: 上报的数据 */
[key: string]: any;
}
而不是根据文档生成的
/** 上报的自定义数据。 */
interface Data {
/** 配置中的字段名 */
key: string;
/** 上报的数据 */
value: any;
}
根据文档中生成的这种写法实际是无效的....就是
{
key:value
}
的写法
版本号:npm install [email protected]
Missing wx.arrayBufferToBase64
and wx.base64ToArrayBuffer
typings.
2017.03.28
A 新增 mac 版开发工具支持蓝牙调试
A 新增 wx.reLaunch wx.chooseAddress wx.openSetting wx.getClipboardData wx.setClipboardData wx.arrayBufferToBase64 wx.base64ToArrayBuffer wx.showLoading wx.hideLoading wx.startAccelerometer wx.stopAccelerometer wx.onNetworkStatusChange wx.hideShareMenu wx.showShareMenu CanvasContext.setTextAlign 等 API 的调试支持
https://developers.weixin.qq.com/miniprogram/dev/devtools/uplog.html#20170328
最近更新的2.7.x 修改了namespace
,这是一个完全的break change.
已经和2.6.x以及之前的 完全不兼容了,甚至导致一些项目build break,(从版本上将完全是两个大版本的变化)。
这种namespace以及interface名称是不是应该有个确定规则?(而不是随意更改)
另外对于break change 能否应该提供一下changelog?
rt.
lineCap
属性应为 'butt' | 'round' | 'square'
而不是 number
lineJoin
属性应为 'bevel' | 'round' | 'miter'
而不是 number
在 vscode 中好像并没有加载出来 wx.cloud 会提示
类型“Wx”上不存在属性“cloud”。
1 按照文档中 Animation Step 方法应返回 Animation,以支持链式调用,现在返回了 void
2 SelectorQuery 中 selectAll 方法缺少 selector 参数
3 Storage 相关方法只定义了基础 object 类型,应使用联合类型 object | string | boolean
4 canvasToTempFilePath 和 createCanvasContext 缺少在组件环境下的 component this 参数
自定义组件支持拓展behaviors
,见自定义组件扩展。
拓展以后可能会新增属性,如官方的computed
:
const beh = require('./behavior.js')
Component({
behaviors: [beh],
data: {
a: 0,
},
computed: {
b() {
return this.data.a + 100
},
},
methods: {
onTap() {
this.setData({
a: ++this.data.a,
})
}
}
})
此时会报错。
rt
encoding?:
| 'ascii'
| 'base64'
| 'binary'
| 'hex'
| 'ucs2/ucs-2/utf16le/utf-16le'
| 'utf-8/utf8'
| 'latin1';
这里的 | 'utf-8/utf8' 应该改为 | 'utf-8' | 'utf8' ,'ucs2/ucs-2/utf16le/utf-16le'也是一样
AuthSetting
type is wrong.
It should be:
interface AuthSetting {
['scope.userInfo']: boolean;
...
}
And here is a use case from wx.getSetting:
wx.getSetting({
success(res) {
console.log(res.authSetting)
// res.authSetting = {
// "scope.userInfo": true,
// "scope.userLocation": true
// }
}
})
Document: https://developers.weixin.qq.com/miniprogram/dev/api/AuthSetting.html
是我用法不对,还是typings支持度不够?
官方文档上sizeType和sourceType的定义类型为Array.string,但定义文件里将这两个参数的类型分别定义为了'original' | 'compressed'和 'album' | 'camera',虽然在调试工具上可以正常使用,但是在iphone上无法正常调用chooseImage,将这两个参数定义修改为('original' | 'compressed') []和('album' | 'camera') []就正常了。
Page 里this.setData
什么时候才能支持去掉this.setData!()
中的感叹号
Add the Typings to https://github.com/DefinitelyTyped/DefinitelyTyped
企业微信的 JD-SDK 里有很多 API 看上去似乎和小程序的一模一样. 请问这些相同的 API 是否可以使用该类型定义文件? 会不会存在虽然 API 名称一致, 但是两者的输入输出类型完全不一样的情况呢?
如题。
RT.
api-typings/types/wx/lib.wx.component.d.ts
Line 108 in f98804c
这里WxPage
找不定义,是改了Page的声明没有上传 ?
重写了 component 定义,除了behaviors 没有测试,其他常用Component的特性和类型绑定基本都支持了:
示例:
Component({
data: {
x: 'xx',
b: {
nn: 1,
bb: false
}
},
properties: {
yy: String, // string 类型
lll: { // number 类型
value: 124,
type: Number,
observer(newVal: number, oldVal: number) {
const x = newVal + oldVal + this.data.b.nn; //observer this 绑定
this.KKKK();
this.setData({
x: 'y',
yy: 'y'
})
}
}
},
lifetimes: {
ready() {
this.KKKK() // lifetimes 调用 methods
//$ExpectError
// this.data.x=1; // data 只读
this.properties.lll; // 访问 properties (number)
this.data;
this.setData({
lll: 12, // setdata
'uu.x': ''
}, (d) => { // callback 参数
d.yy;
d.b.bb;
})
}
},
methods: {
KKKK() {
this.mmm() //函数调用
},
mmm() {
this.dataset.x; //dataset
this.data.x;
},
onTest(e:event.Base){
this.triggerEvent(e.type); // 调用 triggerEvent
}
}
/** [wx.onGyroscopeChange(function callback)](https://developers.weixin.qq.com/miniprogram/dev/api/device/gyroscope/wx.onGyroscopeChange.html)
*
* 监听陀螺仪数据变化事件。频率根据 [wx.startGyroscope()](https://developers.weixin.qq.com/miniprogram/dev/api/device/gyroscope/wx.startGyroscope.html) 的 interval 参数。可以使用 [wx.stopGyroscope()](https://developers.weixin.qq.com/miniprogram/dev/api/device/gyroscope/wx.stopGyroscope.html) 停止监听。
*
* 最低基础库: `2.3.0` */
onGyroscopeChange(
/** 陀螺仪数据变化事件的回调函数 */
callback: OnGyroscopeChangeCallback,
): void;
type OnGyroscopeChangeCallback = (
result: OnGyroscopeChangeCallbackResult,
) => void;
interface OnGyroscopeChangeCallbackResult {
res: Result;
}
interface Result {
/** x 轴的角速度 */
x: number;
/** y 轴的角速度 */
y: number;
/** z 轴的角速度 */
z: number;
}
上面写的是错的,应该去掉result直接将OnGyroscopeChangeCallbackResult
改为
interface OnGyroscopeChangeCallbackResult {
/** x 轴的角速度 */
x: number;
/** y 轴的角速度 */
y: number;
/** z 轴的角速度 */
z: number;
}
或
interface OnGyroscopeChangeCallbackResult extends Result { }
像stopPullDownRefresh(option: StopPullDownRefreshOption): void; 里的option应该改成可空类型,现在调用wx.stopPullDownRefresh({}) 加个空对象参数才不会报错
lib.wx.api.d.ts#L11258
type CanvasGetImageDataSuccessCallback应该是三个参数
width: number,
height: number,
data: Uint8ClampedArray
而不是
result: CanvasGetImageDataSuccessCallbackResult,
data: Uint8ClampedArray,
创建了 @types/miniprogram-wxs 声明wxs
定义npm的包
把 @wechat-miniprogram-admin 添加到了maintainer list里了 ,如果有必要官方可以直接更新这个包
定义 WXS 文件可以直接声明引用
miniprogram-wxs
/// <reference types="miniprogram-wxs"/>
// write typescript code as wxs
const today: Date = getDate();
// javascript code as wxs
var today = getDate();
源文件: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/miniprogram-wxs
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.