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
This commit is contained in:
sporeventexplosion
2019-02-13 20:26:16 +08:00
parent d77c47c753
commit ea07d6040e
2 changed files with 57 additions and 5 deletions
+4 -4
View File
@@ -3745,19 +3745,19 @@ declare namespace wx {
type UnboxBehaviorData<T> = T extends Behavior<{}, {}, {}> ? T['__DO_NOT_USE_INTERNAL_FIELD_DATA'] : {};
type UnboxBehaviorProps<T> = T extends Behavior<{}, {}, {}> ? T['__DO_NOT_USE_INTERNAL_FIELD_PROPS'] : {};
type UnboxBehaviorMethod<T> = T extends Behavior<{}, {}, {}> ? T['__DO_NOT_USE_INTERNAL_FIELD_METHODS'] : {};
type UnboxBehaviorMethods<T> = T extends Behavior<{}, {}, {}> ? T['__DO_NOT_USE_INTERNAL_FIELD_METHODS'] : {};
type UnboxBehaviorsMethods<
Behaviors extends Array<Behavior<{}, {}, {}> | string>
> = UnionToIntersection<UnboxBehaviorMethod<ArrayType<Behaviors>>>;
> = UnboxBehaviorMethods<UnionToIntersection<ArrayType<Behaviors>>>;
type UnboxBehaviorsData<
Behaviors extends Array<Behavior<{}, {}, {}> | string>
> = UnionToIntersection<UnboxBehaviorData<ArrayType<Behaviors>>>;
> = UnboxBehaviorData<UnionToIntersection<ArrayType<Behaviors>>>;
type UnboxBehaviorsProps<
Behaviors extends Array<Behavior<{}, {}, {}> | string>
> = UnionToIntersection<UnboxBehaviorProps<ArrayType<Behaviors>>>;
> = UnboxBehaviorProps<UnionToIntersection<ArrayType<Behaviors>>>;
// CombinedInstance models the `this`, i.e. instance type for (user defined) component
type CombinedInstance<
+53 -1
View File
@@ -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;