Skip to content

Commit

Permalink
feat: pull/333 issuecomment-2333434425 support
Browse files Browse the repository at this point in the history
  • Loading branch information
lv-z-l committed Sep 9, 2024
1 parent 5e6a22b commit 57d4c82
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 16 deletions.
6 changes: 3 additions & 3 deletions test/computed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ ComputedComponent({
})

type IMethods = {
_setData: WechatMiniprogram.Component.InstanceMethods<{}>['setData'],
_setData: WechatMiniprogram.Component.InstanceMethods<{}>['setData']
}
type ICustomProperty = {
_originalSetData: WechatMiniprogram.Component.InstanceMethods<{}>['setData'],
_originalSetData: WechatMiniprogram.Component.InstanceMethods<{}>['setData']
}
Behavior<{}, {}, IMethods, ICustomProperty>({
Behavior<{}, {}, IMethods, [], ICustomProperty>({
lifetimes: {
created() {
this._originalSetData = this.setData
Expand Down
58 changes: 58 additions & 0 deletions test/issue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,62 @@ import WX = WechatMiniprogram
},
}
})
}
// https://github.com/wechat-miniprogram/api-typings/issues/332#issuecomment-2333434425
{
const b1 = Behavior({
properties: {
pA: {
type: String,
value: '',
},
pA1: Boolean
},
data: {
dataA: 'init data',
},
methods: {
methodA() {
return this.data.dataA
},
},
})
const b2 = Behavior({
behaviors: [b1],
properties: {
pB: {
type: Array,
value: [] as string[],
}
},
data: {
dataB: [] as string[],
},
methods: {
methodB() {
return this.data.dataB
},
test() {
expectType<string>(this.data.pA)
expectType<boolean>(this.data.pA1)
expectType<string>(this.data.dataA)
expectType<string>(this.methodA())
},
},
})

Component({
behaviors: [b2],
methods: {
test() {
expectType<string>(this.data.pA)
expectType<boolean>(this.data.pA1)
expectType<string>(this.data.dataA)
expectType<string[]>(this.data.pB)
expectType<string[]>(this.data.dataB)
expectType<string>(this.methodA())
expectType<string[]>(this.methodB())
},
}
})
}
30 changes: 19 additions & 11 deletions types/wx/lib.wx.behavior.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,63 @@ declare namespace WechatMiniprogram.Behavior {
type RealBehaviorType<
TData extends DataOption = {},
TProperty extends PropertyOption = {},
TMethod extends MethodOption = {}
TMethod extends MethodOption = {},
TBehavior extends BehaviorOption = []
> = {
[key in 'BehaviorType']?: {
data: TData
properties: TProperty
methods: TMethod
data: TData & Component.MixinData<TBehavior>
properties: TProperty & Component.MixinProperties<TBehavior, true>
methods: TMethod & Component.MixinMethods<TBehavior>
}
}

type BehaviorIdentifier = string
type Instance<
TData extends DataOption,
TProperty extends PropertyOption,
TMethod extends MethodOption,
TBehavior extends BehaviorOption,
TCustomInstanceProperty extends IAnyObject = Record<string, never>
> = Component.Instance<TData, TProperty, TMethod, [], TCustomInstanceProperty>
type TrivialInstance = Instance<IAnyObject, IAnyObject, IAnyObject>
type TrivialOption = Options<IAnyObject, IAnyObject, IAnyObject>
> = Component.Instance<TData, TProperty, TMethod, TBehavior, TCustomInstanceProperty>
type TrivialInstance = Instance<IAnyObject, IAnyObject, IAnyObject, Component.IAnyArray>
type TrivialOption = Options<IAnyObject, IAnyObject, IAnyObject, Component.IAnyArray>
type Options<
TData extends DataOption,
TProperty extends PropertyOption,
TMethod extends MethodOption,
TBehavior extends BehaviorOption,
TCustomInstanceProperty extends IAnyObject = Record<string, never>
> = Partial<Data<TData>> &
Partial<Property<TProperty>> &
Partial<Method<TMethod>> &
Partial<Behavior<TBehavior>> &
Partial<OtherOption> &
Partial<Lifetimes> &
ThisType<Instance<TData, TProperty, TMethod, TCustomInstanceProperty>>
ThisType<Instance<TData, TProperty, TMethod, TBehavior, TCustomInstanceProperty>>
interface Constructor {
<
TData extends DataOption,
TProperty extends PropertyOption,
TMethod extends MethodOption,
TBehavior extends BehaviorOption,
TCustomInstanceProperty extends IAnyObject = Record<string, never>
>(
options: Options<TData, TProperty, TMethod, TCustomInstanceProperty>
): BehaviorIdentifier & RealBehaviorType<TData, TProperty, TMethod>
options: Options<TData, TProperty, TMethod, TBehavior, TCustomInstanceProperty>
): BehaviorIdentifier & RealBehaviorType<TData, TProperty, TMethod, TBehavior>
}

type DataOption = Component.DataOption
type PropertyOption = Component.PropertyOption
type MethodOption = Component.MethodOption
type BehaviorOption = Component.BehaviorOption
type Data<D extends DataOption> = Component.Data<D>
type Property<P extends PropertyOption> = Component.Property<P>
type Method<M extends MethodOption> = Component.Method<M>
type Behavior<B extends BehaviorOption> = Component.Behavior<B>

type DefinitionFilter = Component.DefinitionFilter
type Lifetimes = Component.Lifetimes
type OtherOption = Omit<Component.OtherOption, 'options'> & { behaviors: BehaviorIdentifier[]}
type OtherOption = Omit<Component.OtherOption, 'options'>
}
/** 注册一个 `behavior`,接受一个 `Object` 类型的参数。*/
declare let Behavior: WechatMiniprogram.Behavior.Constructor
5 changes: 3 additions & 2 deletions types/wx/lib.wx.component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ declare namespace WechatMiniprogram.Component {
type BehaviorOption = Behavior.BehaviorIdentifier[]
type ExtractBehaviorType<T> = T extends { BehaviorType?: infer B } ? B : never
type ExtractData<T> = T extends { data: infer D } ? D : never
type ExtractProperties<T> = T extends { properties: infer P } ? PropertyOptionToData<P extends PropertyOption ? P : {}> : never
type ExtractProperties<T, TIsBehavior extends boolean = false> = T extends { properties: infer P } ?
TIsBehavior extends true ? P : PropertyOptionToData<P extends PropertyOption ? P : {}> : never
type ExtractMethods<T> = T extends { methods: infer M } ? M : never
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never
type MixinData<T extends any[]> = UnionToIntersection<ExtractData<ExtractBehaviorType<T[number]>>>
type MixinProperties<T extends any[]> = UnionToIntersection<ExtractProperties<ExtractBehaviorType<T[number]>>>
type MixinProperties<T extends any[], TIsBehavior extends boolean = false> = UnionToIntersection<ExtractProperties<ExtractBehaviorType<T[number]>, TIsBehavior>>
type MixinMethods<T extends any[]> = UnionToIntersection<ExtractMethods<ExtractBehaviorType<T[number]>>>

interface Behavior<B extends BehaviorOption> {
Expand Down

0 comments on commit 57d4c82

Please sign in to comment.