From ea07d6040edab87188c806ee8d3094595f36b618 Mon Sep 17 00:00:00 2001 From: sporeventexplosion Date: Wed, 13 Feb 2019 19:20:52 +0800 Subject: [PATCH] support union types for the properties/data/methods of behaviors union types in properties/data/methods are no longer converted to intersection types when a behavior is included in another behavior/component --- types/weixin-app/index.d.ts | 8 ++--- types/weixin-app/weixin-app-tests.ts | 54 +++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/types/weixin-app/index.d.ts b/types/weixin-app/index.d.ts index c4c46353d0..e0c5f51598 100644 --- a/types/weixin-app/index.d.ts +++ b/types/weixin-app/index.d.ts @@ -3745,19 +3745,19 @@ declare namespace wx { type UnboxBehaviorData = T extends Behavior<{}, {}, {}> ? T['__DO_NOT_USE_INTERNAL_FIELD_DATA'] : {}; type UnboxBehaviorProps = T extends Behavior<{}, {}, {}> ? T['__DO_NOT_USE_INTERNAL_FIELD_PROPS'] : {}; - type UnboxBehaviorMethod = T extends Behavior<{}, {}, {}> ? T['__DO_NOT_USE_INTERNAL_FIELD_METHODS'] : {}; + type UnboxBehaviorMethods = T extends Behavior<{}, {}, {}> ? T['__DO_NOT_USE_INTERNAL_FIELD_METHODS'] : {}; type UnboxBehaviorsMethods< Behaviors extends Array | string> - > = UnionToIntersection>>; + > = UnboxBehaviorMethods>>; type UnboxBehaviorsData< Behaviors extends Array | string> - > = UnionToIntersection>>; + > = UnboxBehaviorData>>; type UnboxBehaviorsProps< Behaviors extends Array | string> - > = UnionToIntersection>>; + > = UnboxBehaviorProps>>; // CombinedInstance models the `this`, i.e. instance type for (user defined) component type CombinedInstance< diff --git a/types/weixin-app/weixin-app-tests.ts b/types/weixin-app/weixin-app-tests.ts index d6265f3798..fb717d0c31 100644 --- a/types/weixin-app/weixin-app-tests.ts +++ b/types/weixin-app/weixin-app-tests.ts @@ -25,8 +25,46 @@ const parentBehavior = Behavior({ } }); +function createBehaviorWithUnionTypes(n: number) { + const properties = n % 2 < 1 ? { + unionPropA: { + type: String, + }, + } : { + unionPropB: { + type: Number, + }, + }; + + const data = n % 4 < 2 ? { + unionDataA: 'a', + } : { + unionDataB: 1, + }; + + const methods = n % 8 < 4 ? { + unionMethodA(a: number) { + return n + 1; + }, + } : { + unionMethodB(a: string) { + return {value: a}; + }, + }; + + return Behavior({ + properties, + data, + methods + }); +} + const behavior = Behavior({ - behaviors: [parentBehavior, "wx://form-field"], + behaviors: [ + createBehaviorWithUnionTypes(1), + parentBehavior, + "wx://form-field", + ], properties: { myBehaviorProperty: { type: String @@ -126,6 +164,20 @@ Component({ console.log(this.data.myParentBehaviorData); console.log(this.data.myParentBehaviorProperty); this.myParentBehaviorMethod(456); + if (this.unionMethodA) { + console.log(this.unionMethodA(5)); + } + if (this.unionMethodB) { + console.log(this.unionMethodB('test').value); + } + console.log(this.data.unionDataA); + console.log(this.data.unionDataB); + console.log(this.data.unionPropA); + console.log(this.data.unionPropB); + console.log(this.properties.unionDataA); + console.log(this.properties.unionDataB); + console.log(this.properties.unionPropA); + console.log(this.properties.unionPropB); }, readMyDataAndMyProps() { const stringValue1: string = this.data.myProperty;