Skip to content

Commit 3d91e03

Browse files
committed
fix: getOpenerEventChannel may return an empty object
1 parent 335d8a2 commit 3d91e03

File tree

6 files changed

+68
-18
lines changed

6 files changed

+68
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2025-07-15 v4.1.0
2+
- 修改 `getOpenerEventChannel` 的返回值的类型,使其有可能返回空对象,以正确反映实际情况。这是一个 **破坏性改动**`getOpenerEventChannel` 的返回值上的 `emit`, `on` 等方法不再可以直接调用 ([#350](https://github.com/wechat-miniprogram/api-typings/issues/350))
3+
- 解决依赖安全问题
4+
15
## 2025-07-15 v4.0.8
26
- 更新 API 定义到 3.8.10
37

test/api-doc.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3096,12 +3096,14 @@ type TPlatform = 'ios' | 'android' | 'windows' | 'mac' | 'devtools' | 'ohos'
30963096
onLoad(option) {
30973097
console.log(option.query)
30983098
const eventChannel = this.getOpenerEventChannel()
3099-
eventChannel.emit('acceptDataFromOpenedPage', { data: 'test' })
3100-
eventChannel.emit('someEvent', { data: 'test' })
3101-
// 监听acceptDataFromOpenerPage事件,获取上一页面通过eventChannel传送到当前页面的数据
3102-
eventChannel.on('acceptDataFromOpenerPage', function(data: any) {
3103-
console.log(data)
3104-
})
3099+
if (typeof eventChannel.emit === 'function') {
3100+
eventChannel.emit('acceptDataFromOpenedPage', { data: 'test' })
3101+
eventChannel.emit('someEvent', { data: 'test' })
3102+
// 监听acceptDataFromOpenerPage事件,获取上一页面通过eventChannel传送到当前页面的数据
3103+
eventChannel.on('acceptDataFromOpenerPage', function(data: any) {
3104+
console.log(data)
3105+
})
3106+
}
31053107
},
31063108
})
31073109
}

test/component.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expectType, expectError } from 'tsd'
1+
import { expectType, expectError, expectNotAssignable } from 'tsd'
22

33
expectType<string>(Component({}))
44

@@ -410,10 +410,15 @@ Component({
410410
methods: {
411411
test() {
412412
const channel = this.getOpenerEventChannel()
413-
expectType<WechatMiniprogram.EventChannel>(channel)
414-
channel.emit('test', {})
415-
channel.on('xxx', () => {})
416-
expectError(channel.emit(1, 2))
413+
// `tsd` not yet supports error `ts2722`
414+
// expectError(channel.emit('test', {}))
415+
expectNotAssignable<(...args: any[]) => any>(channel.emit)
416+
channel.emit?.('test', {})
417+
expectError(channel.emit?.(1, 2))
418+
channel.on?.('xxx', () => {})
419+
if (typeof channel.emit === 'function') {
420+
channel.on('xxx', () => {})
421+
}
417422
},
418423
},
419424
})

test/page.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expectType, expectError } from 'tsd'
1+
import { expectType, expectError, expectNotAssignable } from 'tsd'
22

33
expectType<void>(Page({}))
44

@@ -203,10 +203,15 @@ Page<DataType, CustomOption>({
203203
Page({
204204
test() {
205205
const channel = this.getOpenerEventChannel()
206-
expectType<WechatMiniprogram.EventChannel>(channel)
207-
channel.emit('test', {})
208-
channel.on('xxx', () => {})
209-
expectError(channel.emit(1, 2))
206+
// `tsd` not yet supports error `ts2722`
207+
// expectError(channel.emit('test', {}))
208+
expectNotAssignable<(...args: any[]) => any>(channel.emit)
209+
channel.emit?.('test', {})
210+
expectError(channel.emit?.(1, 2))
211+
channel.on?.('xxx', () => {})
212+
if (typeof channel.emit === 'function') {
213+
channel.on('xxx', () => {})
214+
}
210215
},
211216
})
212217

types/wx/lib.wx.api.d.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10573,7 +10573,7 @@ NFCAdapter.offDiscovered(listener) // 需传入与监听时同一个的函数对
1057310573
success?: RequestSubscribeMessageSuccessCallback
1057410574
}
1057510575
interface RequestSubscribeMessageSuccessCallbackResult {
10576-
/** [TEMPLATE_ID]是动态的键,即模板id,值包括'accept'、'reject'、'ban'、'filter'、'acceptWithAlert'、'acceptWithAudio'。'accept'表示用户同意订阅该条id对应的模板消息,'reject'表示用户拒绝订阅该条id对应的模板消息,'ban'表示已被后台封禁,'filter'表示该模板因为模板标题同名被后台过滤,'acceptWithAlert' 表示用户接收消息并打开提醒, 'acceptWithAudio' 表示用户接收订阅消息并开启了语音提醒, 'acceptWithForcePush' 表示用户接收订阅消息并开启了推送提醒。例如 { errMsg: "requestSubscribeMessage:ok", zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE: "accept"} 表示用户同意订阅zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE这条消息 */
10576+
/** [TEMPLATE_ID]是动态的键,即模板id,值包括'accept'、'reject'、'ban'、'filter'。'accept'表示用户同意订阅该条id对应的模板消息,'reject'表示用户拒绝订阅该条id对应的模板消息,'ban'表示已被后台封禁,'filter'表示该模板因为模板标题同名被后台过滤。例如 { errMsg: "requestSubscribeMessage:ok", zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE: "accept"} 表示用户同意订阅zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE这条消息 */
1057710577
[TEMPLATE_ID: string]: string
1057810578
/** 接口调用成功时errMsg值为'requestSubscribeMessage:ok' */
1057910579
errMsg: string
@@ -15916,6 +15916,40 @@ this.editorCtx.insertImage({
1591615916
* 撤销 */
1591715917
undo(option?: UndoOption): void
1591815918
}
15919+
interface EmptyEventChannel {
15920+
/** [EventChannel.emit(string eventName, any args)](https://developers.weixin.qq.com/miniprogram/dev/api/route/EventChannel.emit.html)
15921+
*
15922+
* 需要基础库: `2.7.3`
15923+
*
15924+
* 在插件中使用:支持
15925+
*
15926+
* 触发一个事件 */
15927+
emit: undefined
15928+
/** [EventChannel.off(string eventName, function fn)](https://developers.weixin.qq.com/miniprogram/dev/api/route/EventChannel.off.html)
15929+
*
15930+
* 需要基础库: `2.7.3`
15931+
*
15932+
* 在插件中使用:支持
15933+
*
15934+
* 取消监听一个事件。给出第二个参数时,只取消给出的监听函数,否则取消所有监听函数 */
15935+
off: undefined
15936+
/** [EventChannel.on(string eventName, function fn)](https://developers.weixin.qq.com/miniprogram/dev/api/route/EventChannel.on.html)
15937+
*
15938+
* 需要基础库: `2.7.3`
15939+
*
15940+
* 在插件中使用:支持
15941+
*
15942+
* 持续监听一个事件 */
15943+
on: undefined
15944+
/** [EventChannel.once(string eventName, function fn)](https://developers.weixin.qq.com/miniprogram/dev/api/route/EventChannel.once.html)
15945+
*
15946+
* 需要基础库: `2.7.3`
15947+
*
15948+
* 在插件中使用:支持
15949+
*
15950+
* 监听一个事件一次,触发后失效 */
15951+
once: undefined
15952+
}
1591915953
interface EntryList {
1592015954
/** [Array.&lt;[PerformanceEntry](https://developers.weixin.qq.com/miniprogram/dev/api/base/performance/PerformanceEntry.html)&gt; EntryList.getEntries()](https://developers.weixin.qq.com/miniprogram/dev/api/base/performance/EntryList.getEntries.html)
1592115955
*

types/wx/lib.wx.component.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ declare namespace WechatMiniprogram.Component {
325325
*
326326
* 最低基础库版本:[`2.7.3`](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html)
327327
*/
328-
getOpenerEventChannel(): EventChannel
328+
getOpenerEventChannel(): EventChannel | EmptyEventChannel
329329
/**
330330
* 绑定由 worklet 驱动的样式到相应的节点,详见 [worklet 动画](https://developers.weixin.qq.com/miniprogram/dev/framework/runtime/skyline/worklet.html)
331331
*

0 commit comments

Comments
 (0)