Skip to content

Commit 57d4c82

Browse files
committed
feat: pull/333 issuecomment-2333434425 support
1 parent 5e6a22b commit 57d4c82

File tree

4 files changed

+83
-16
lines changed

4 files changed

+83
-16
lines changed

test/computed.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ ComputedComponent({
2525
})
2626

2727
type IMethods = {
28-
_setData: WechatMiniprogram.Component.InstanceMethods<{}>['setData'],
28+
_setData: WechatMiniprogram.Component.InstanceMethods<{}>['setData']
2929
}
3030
type ICustomProperty = {
31-
_originalSetData: WechatMiniprogram.Component.InstanceMethods<{}>['setData'],
31+
_originalSetData: WechatMiniprogram.Component.InstanceMethods<{}>['setData']
3232
}
33-
Behavior<{}, {}, IMethods, ICustomProperty>({
33+
Behavior<{}, {}, IMethods, [], ICustomProperty>({
3434
lifetimes: {
3535
created() {
3636
this._originalSetData = this.setData

test/issue.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,4 +540,62 @@ import WX = WechatMiniprogram
540540
},
541541
}
542542
})
543+
}
544+
// https://github.com/wechat-miniprogram/api-typings/issues/332#issuecomment-2333434425
545+
{
546+
const b1 = Behavior({
547+
properties: {
548+
pA: {
549+
type: String,
550+
value: '',
551+
},
552+
pA1: Boolean
553+
},
554+
data: {
555+
dataA: 'init data',
556+
},
557+
methods: {
558+
methodA() {
559+
return this.data.dataA
560+
},
561+
},
562+
})
563+
const b2 = Behavior({
564+
behaviors: [b1],
565+
properties: {
566+
pB: {
567+
type: Array,
568+
value: [] as string[],
569+
}
570+
},
571+
data: {
572+
dataB: [] as string[],
573+
},
574+
methods: {
575+
methodB() {
576+
return this.data.dataB
577+
},
578+
test() {
579+
expectType<string>(this.data.pA)
580+
expectType<boolean>(this.data.pA1)
581+
expectType<string>(this.data.dataA)
582+
expectType<string>(this.methodA())
583+
},
584+
},
585+
})
586+
587+
Component({
588+
behaviors: [b2],
589+
methods: {
590+
test() {
591+
expectType<string>(this.data.pA)
592+
expectType<boolean>(this.data.pA1)
593+
expectType<string>(this.data.dataA)
594+
expectType<string[]>(this.data.pB)
595+
expectType<string[]>(this.data.dataB)
596+
expectType<string>(this.methodA())
597+
expectType<string[]>(this.methodB())
598+
},
599+
}
600+
})
543601
}

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,55 +24,63 @@ declare namespace WechatMiniprogram.Behavior {
2424
type RealBehaviorType<
2525
TData extends DataOption = {},
2626
TProperty extends PropertyOption = {},
27-
TMethod extends MethodOption = {}
27+
TMethod extends MethodOption = {},
28+
TBehavior extends BehaviorOption = []
2829
> = {
2930
[key in 'BehaviorType']?: {
30-
data: TData
31-
properties: TProperty
32-
methods: TMethod
31+
data: TData & Component.MixinData<TBehavior>
32+
properties: TProperty & Component.MixinProperties<TBehavior, true>
33+
methods: TMethod & Component.MixinMethods<TBehavior>
3334
}
3435
}
36+
3537
type BehaviorIdentifier = string
3638
type Instance<
3739
TData extends DataOption,
3840
TProperty extends PropertyOption,
3941
TMethod extends MethodOption,
42+
TBehavior extends BehaviorOption,
4043
TCustomInstanceProperty extends IAnyObject = Record<string, never>
41-
> = Component.Instance<TData, TProperty, TMethod, [], TCustomInstanceProperty>
42-
type TrivialInstance = Instance<IAnyObject, IAnyObject, IAnyObject>
43-
type TrivialOption = Options<IAnyObject, IAnyObject, IAnyObject>
44+
> = Component.Instance<TData, TProperty, TMethod, TBehavior, TCustomInstanceProperty>
45+
type TrivialInstance = Instance<IAnyObject, IAnyObject, IAnyObject, Component.IAnyArray>
46+
type TrivialOption = Options<IAnyObject, IAnyObject, IAnyObject, Component.IAnyArray>
4447
type Options<
4548
TData extends DataOption,
4649
TProperty extends PropertyOption,
4750
TMethod extends MethodOption,
51+
TBehavior extends BehaviorOption,
4852
TCustomInstanceProperty extends IAnyObject = Record<string, never>
4953
> = Partial<Data<TData>> &
5054
Partial<Property<TProperty>> &
5155
Partial<Method<TMethod>> &
56+
Partial<Behavior<TBehavior>> &
5257
Partial<OtherOption> &
5358
Partial<Lifetimes> &
54-
ThisType<Instance<TData, TProperty, TMethod, TCustomInstanceProperty>>
59+
ThisType<Instance<TData, TProperty, TMethod, TBehavior, TCustomInstanceProperty>>
5560
interface Constructor {
5661
<
5762
TData extends DataOption,
5863
TProperty extends PropertyOption,
5964
TMethod extends MethodOption,
65+
TBehavior extends BehaviorOption,
6066
TCustomInstanceProperty extends IAnyObject = Record<string, never>
6167
>(
62-
options: Options<TData, TProperty, TMethod, TCustomInstanceProperty>
63-
): BehaviorIdentifier & RealBehaviorType<TData, TProperty, TMethod>
68+
options: Options<TData, TProperty, TMethod, TBehavior, TCustomInstanceProperty>
69+
): BehaviorIdentifier & RealBehaviorType<TData, TProperty, TMethod, TBehavior>
6470
}
6571

6672
type DataOption = Component.DataOption
6773
type PropertyOption = Component.PropertyOption
6874
type MethodOption = Component.MethodOption
75+
type BehaviorOption = Component.BehaviorOption
6976
type Data<D extends DataOption> = Component.Data<D>
7077
type Property<P extends PropertyOption> = Component.Property<P>
7178
type Method<M extends MethodOption> = Component.Method<M>
79+
type Behavior<B extends BehaviorOption> = Component.Behavior<B>
7280

7381
type DefinitionFilter = Component.DefinitionFilter
7482
type Lifetimes = Component.Lifetimes
75-
type OtherOption = Omit<Component.OtherOption, 'options'> & { behaviors: BehaviorIdentifier[]}
83+
type OtherOption = Omit<Component.OtherOption, 'options'>
7684
}
7785
/** 注册一个 `behavior`,接受一个 `Object` 类型的参数。*/
7886
declare let Behavior: WechatMiniprogram.Behavior.Constructor

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@ declare namespace WechatMiniprogram.Component {
100100
type BehaviorOption = Behavior.BehaviorIdentifier[]
101101
type ExtractBehaviorType<T> = T extends { BehaviorType?: infer B } ? B : never
102102
type ExtractData<T> = T extends { data: infer D } ? D : never
103-
type ExtractProperties<T> = T extends { properties: infer P } ? PropertyOptionToData<P extends PropertyOption ? P : {}> : never
103+
type ExtractProperties<T, TIsBehavior extends boolean = false> = T extends { properties: infer P } ?
104+
TIsBehavior extends true ? P : PropertyOptionToData<P extends PropertyOption ? P : {}> : never
104105
type ExtractMethods<T> = T extends { methods: infer M } ? M : never
105106
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never
106107
type MixinData<T extends any[]> = UnionToIntersection<ExtractData<ExtractBehaviorType<T[number]>>>
107-
type MixinProperties<T extends any[]> = UnionToIntersection<ExtractProperties<ExtractBehaviorType<T[number]>>>
108+
type MixinProperties<T extends any[], TIsBehavior extends boolean = false> = UnionToIntersection<ExtractProperties<ExtractBehaviorType<T[number]>, TIsBehavior>>
108109
type MixinMethods<T extends any[]> = UnionToIntersection<ExtractMethods<ExtractBehaviorType<T[number]>>>
109110

110111
interface Behavior<B extends BehaviorOption> {

0 commit comments

Comments
 (0)