mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 23:07:06 +00:00
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:
Vendored
+4
-4
@@ -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<
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user