Skip to content

Commit 32d8f29

Browse files
authored
Merge pull request #334 from lv-z-l/master
fix(behavior类型支持迭代):修复behavior中不包含data/properties时,会导致Component this.data/this.properties上访问未定义属性不抛错误
2 parents 6b8e1d3 + b7e6fe5 commit 32d8f29

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

test/issue.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,4 +598,40 @@ import WX = WechatMiniprogram
598598
},
599599
}
600600
})
601+
}
602+
603+
{
604+
const b1 = Behavior({
605+
methods: {
606+
methodA() {
607+
return ['']
608+
},
609+
},
610+
})
611+
const b2 = Behavior({
612+
properties: {
613+
pB: {
614+
type: Array,
615+
value: [] as string[],
616+
}
617+
}
618+
})
619+
const b3 = Behavior({
620+
data: {
621+
dataC: 'init data',
622+
}
623+
})
624+
625+
Component({
626+
behaviors: [b1, b2, b3],
627+
methods: {
628+
test() {
629+
expectType<string[]>(this.data.pB)
630+
expectType<string>(this.data.dataC)
631+
expectType<string[]>(this.methodA())
632+
expectError(this.data.xxx)
633+
expectError(this.properties.yyy)
634+
},
635+
}
636+
})
601637
}

types/wx/lib.wx.behavior.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ declare namespace WechatMiniprogram.Behavior {
2929
TBehavior extends BehaviorOption = []
3030
> = string & {
3131
[key in 'BehaviorType']?: {
32-
data: TData & Component.MixinData<TBehavior>
33-
properties: TProperty & Component.MixinProperties<TBehavior, true>
34-
methods: TMethod & Component.MixinMethods<TBehavior>
32+
data: Component.FilterUnknownType<TData> & Component.MixinData<TBehavior>
33+
properties: Component.FilterUnknownType<TProperty> & Component.MixinProperties<TBehavior, true>
34+
methods: Component.FilterUnknownType<TMethod> & Component.MixinMethods<TBehavior>
3535
}
3636
}
3737
type Instance<

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ SOFTWARE.
2121
***************************************************************************** */
2222

2323
declare namespace WechatMiniprogram.Component {
24-
type FilterUnknownProperty<TProperty> = string extends keyof TProperty ? {} : TProperty
24+
type FilterUnknownType<T> = string extends keyof T ? {} : T
2525
type Instance<
2626
TData extends DataOption,
2727
TProperty extends PropertyOption,
@@ -36,11 +36,11 @@ declare namespace WechatMiniprogram.Component {
3636
(TIsPage extends true ? Page.ILifetime : {}) &
3737
Omit<TCustomInstanceProperty, 'properties' | 'methods' | 'data'> & {
3838
/** 组件数据,**包括内部数据和属性值** */
39-
data: TData & MixinData<TBehavior> &
40-
MixinProperties<TBehavior> & PropertyOptionToData<FilterUnknownProperty<TProperty>>
39+
data: FilterUnknownType<TData> & MixinData<TBehavior> &
40+
MixinProperties<TBehavior> & PropertyOptionToData<FilterUnknownType<TProperty>>
4141
/** 组件数据,**包括内部数据和属性值**(与 `data` 一致) */
42-
properties: TData & MixinData<TBehavior> &
43-
MixinProperties<TBehavior> & PropertyOptionToData<FilterUnknownProperty<TProperty>>
42+
properties: FilterUnknownType<TData> & MixinData<TBehavior> &
43+
MixinProperties<TBehavior> & PropertyOptionToData<FilterUnknownType<TProperty>>
4444
}
4545

4646
type IEmptyArray = []

0 commit comments

Comments
 (0)