Skip to content

Commit

Permalink
Merge pull request #334 from lv-z-l/master
Browse files Browse the repository at this point in the history
fix(behavior类型支持迭代):修复behavior中不包含data/properties时,会导致Component this.data/this.properties上访问未定义属性不抛错误
  • Loading branch information
SgLy authored Sep 24, 2024
2 parents 6b8e1d3 + b7e6fe5 commit 32d8f29
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
36 changes: 36 additions & 0 deletions test/issue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,40 @@ import WX = WechatMiniprogram
},
}
})
}

{
const b1 = Behavior({
methods: {
methodA() {
return ['']
},
},
})
const b2 = Behavior({
properties: {
pB: {
type: Array,
value: [] as string[],
}
}
})
const b3 = Behavior({
data: {
dataC: 'init data',
}
})

Component({
behaviors: [b1, b2, b3],
methods: {
test() {
expectType<string[]>(this.data.pB)
expectType<string>(this.data.dataC)
expectType<string[]>(this.methodA())
expectError(this.data.xxx)
expectError(this.properties.yyy)
},
}
})
}
6 changes: 3 additions & 3 deletions types/wx/lib.wx.behavior.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ declare namespace WechatMiniprogram.Behavior {
TBehavior extends BehaviorOption = []
> = string & {
[key in 'BehaviorType']?: {
data: TData & Component.MixinData<TBehavior>
properties: TProperty & Component.MixinProperties<TBehavior, true>
methods: TMethod & Component.MixinMethods<TBehavior>
data: Component.FilterUnknownType<TData> & Component.MixinData<TBehavior>
properties: Component.FilterUnknownType<TProperty> & Component.MixinProperties<TBehavior, true>
methods: Component.FilterUnknownType<TMethod> & Component.MixinMethods<TBehavior>
}
}
type Instance<
Expand Down
10 changes: 5 additions & 5 deletions types/wx/lib.wx.component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SOFTWARE.
***************************************************************************** */

declare namespace WechatMiniprogram.Component {
type FilterUnknownProperty<TProperty> = string extends keyof TProperty ? {} : TProperty
type FilterUnknownType<T> = string extends keyof T ? {} : T
type Instance<
TData extends DataOption,
TProperty extends PropertyOption,
Expand All @@ -36,11 +36,11 @@ declare namespace WechatMiniprogram.Component {
(TIsPage extends true ? Page.ILifetime : {}) &
Omit<TCustomInstanceProperty, 'properties' | 'methods' | 'data'> & {
/** 组件数据,**包括内部数据和属性值** */
data: TData & MixinData<TBehavior> &
MixinProperties<TBehavior> & PropertyOptionToData<FilterUnknownProperty<TProperty>>
data: FilterUnknownType<TData> & MixinData<TBehavior> &
MixinProperties<TBehavior> & PropertyOptionToData<FilterUnknownType<TProperty>>
/** 组件数据,**包括内部数据和属性值**(与 `data` 一致) */
properties: TData & MixinData<TBehavior> &
MixinProperties<TBehavior> & PropertyOptionToData<FilterUnknownProperty<TProperty>>
properties: FilterUnknownType<TData> & MixinData<TBehavior> &
MixinProperties<TBehavior> & PropertyOptionToData<FilterUnknownType<TProperty>>
}

type IEmptyArray = []
Expand Down

0 comments on commit 32d8f29

Please sign in to comment.