Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(behavior类型支持迭代):修复behavior中不包含data/properties时,会导致Component this.data/this.properties上访问未定义属性不抛错误 #334

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading