From 57d4c82e8252ca2105fffe177c8fe1d42bc6fb75 Mon Sep 17 00:00:00 2001 From: lvzl <627417163@qq.com> Date: Mon, 9 Sep 2024 10:50:33 +0800 Subject: [PATCH] feat: pull/333 issuecomment-2333434425 support --- test/computed.test.ts | 6 ++-- test/issue.test.ts | 58 ++++++++++++++++++++++++++++++++++ types/wx/lib.wx.behavior.d.ts | 30 +++++++++++------- types/wx/lib.wx.component.d.ts | 5 +-- 4 files changed, 83 insertions(+), 16 deletions(-) diff --git a/test/computed.test.ts b/test/computed.test.ts index d216ada..bef52de 100644 --- a/test/computed.test.ts +++ b/test/computed.test.ts @@ -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 diff --git a/test/issue.test.ts b/test/issue.test.ts index 79d69ef..748970d 100644 --- a/test/issue.test.ts +++ b/test/issue.test.ts @@ -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(this.data.pA) + expectType(this.data.pA1) + expectType(this.data.dataA) + expectType(this.methodA()) + }, + }, + }) + + Component({ + behaviors: [b2], + methods: { + test() { + expectType(this.data.pA) + expectType(this.data.pA1) + expectType(this.data.dataA) + expectType(this.data.pB) + expectType(this.data.dataB) + expectType(this.methodA()) + expectType(this.methodB()) + }, + } + }) } \ No newline at end of file diff --git a/types/wx/lib.wx.behavior.d.ts b/types/wx/lib.wx.behavior.d.ts index ca597cf..81176a4 100644 --- a/types/wx/lib.wx.behavior.d.ts +++ b/types/wx/lib.wx.behavior.d.ts @@ -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 + properties: TProperty & Component.MixinProperties + methods: TMethod & Component.MixinMethods } } + type BehaviorIdentifier = string type Instance< TData extends DataOption, TProperty extends PropertyOption, TMethod extends MethodOption, + TBehavior extends BehaviorOption, TCustomInstanceProperty extends IAnyObject = Record - > = Component.Instance - type TrivialInstance = Instance - type TrivialOption = Options + > = Component.Instance + type TrivialInstance = Instance + type TrivialOption = Options type Options< TData extends DataOption, TProperty extends PropertyOption, TMethod extends MethodOption, + TBehavior extends BehaviorOption, TCustomInstanceProperty extends IAnyObject = Record > = Partial> & Partial> & Partial> & + Partial> & Partial & Partial & - ThisType> + ThisType> interface Constructor { < TData extends DataOption, TProperty extends PropertyOption, TMethod extends MethodOption, + TBehavior extends BehaviorOption, TCustomInstanceProperty extends IAnyObject = Record >( - options: Options - ): BehaviorIdentifier & RealBehaviorType + options: Options + ): BehaviorIdentifier & RealBehaviorType } type DataOption = Component.DataOption type PropertyOption = Component.PropertyOption type MethodOption = Component.MethodOption + type BehaviorOption = Component.BehaviorOption type Data = Component.Data type Property

= Component.Property

type Method = Component.Method + type Behavior = Component.Behavior type DefinitionFilter = Component.DefinitionFilter type Lifetimes = Component.Lifetimes - type OtherOption = Omit & { behaviors: BehaviorIdentifier[]} + type OtherOption = Omit } /** 注册一个 `behavior`,接受一个 `Object` 类型的参数。*/ declare let Behavior: WechatMiniprogram.Behavior.Constructor diff --git a/types/wx/lib.wx.component.d.ts b/types/wx/lib.wx.component.d.ts index 93da5c1..d724ed3 100644 --- a/types/wx/lib.wx.component.d.ts +++ b/types/wx/lib.wx.component.d.ts @@ -100,11 +100,12 @@ declare namespace WechatMiniprogram.Component { type BehaviorOption = Behavior.BehaviorIdentifier[] type ExtractBehaviorType = T extends { BehaviorType?: infer B } ? B : never type ExtractData = T extends { data: infer D } ? D : never - type ExtractProperties = T extends { properties: infer P } ? PropertyOptionToData

: never + type ExtractProperties = T extends { properties: infer P } ? + TIsBehavior extends true ? P : PropertyOptionToData

: never type ExtractMethods = T extends { methods: infer M } ? M : never type UnionToIntersection = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never type MixinData = UnionToIntersection>> - type MixinProperties = UnionToIntersection>> + type MixinProperties = UnionToIntersection, TIsBehavior>> type MixinMethods = UnionToIntersection>> interface Behavior {