Skip to content

Commit 3af2210

Browse files
committed
feat: 优化实现
1 parent f78974b commit 3af2210

File tree

5 files changed

+13
-19
lines changed

5 files changed

+13
-19
lines changed

test/behavior.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { expectType } from 'tsd'
22

3-
expectType<{
4-
data: {}
5-
properties: {}
6-
methods: {}
7-
}>(Behavior({}))
3+
expectType<string & { BehaviorType: { data: {}, properties: {}, methods: {} } }>(Behavior({}))
84

95
Behavior({
106
behaviors: [],

test/component.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { expectType, expectError } from 'tsd'
33
expectType<string>(Component({}))
44

55
Component({
6-
behaviors: [{data: {}, properties: {}, methods: {}}],
6+
behaviors: [],
77

88
properties: {
99
myProperty: {

test/issue.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,7 @@ import WX = WechatMiniprogram
285285
type IMethod = {
286286
setJob(job: string): void
287287
}
288-
type IBehavior = [{
289-
data: {}
290-
properties: {}
291-
methods: {}
292-
}]
288+
type IBehavior = []
293289
type ICustomInstanceProperty = {
294290
job: string
295291
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ declare namespace WechatMiniprogram.Behavior {
2525
TData extends DataOption = {},
2626
TProperty extends PropertyOption = {},
2727
TMethod extends MethodOption = {}
28-
> = {
29-
data: TData
30-
properties: TProperty
31-
methods: TMethod
28+
> = string & {
29+
BehaviorType: {
30+
data: TData
31+
properties: TProperty
32+
methods: TMethod
33+
}
3234
}
3335
type Instance<
3436
TData extends DataOption,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ declare namespace WechatMiniprogram.Component {
9797
type PropertyOption = Record<string, AllProperty>
9898
type MethodOption = Record<string, Function>
9999

100-
101100
type BehaviorOption = Behavior.BehaviorIdentifier[]
101+
type ExtractBehaviorType<T> = T extends { BehaviorType: infer B } ? B : never
102102
type ExtractData<T> = T extends { data: infer D } ? D : never
103103
type ExtractProperties<T> = T extends { properties: infer P } ? PropertyOptionToData<P extends PropertyOption ? P : {}> : never
104104
type ExtractMethods<T> = T extends { methods: infer M } ? M : never
105105
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never
106-
type MixinData<T extends any[]> = UnionToIntersection<ExtractData<T[number]>>
107-
type MixinProperties<T extends any[]> = UnionToIntersection<ExtractProperties<T[number]>>
108-
type MixinMethods<T extends any[]> = UnionToIntersection<ExtractMethods<T[number]>>
106+
type MixinData<T extends any[]> = UnionToIntersection<ExtractData<ExtractBehaviorType<T[number]>>>
107+
type MixinProperties<T extends any[]> = UnionToIntersection<ExtractProperties<ExtractBehaviorType<T[number]>>>
108+
type MixinMethods<T extends any[]> = UnionToIntersection<ExtractMethods<ExtractBehaviorType<T[number]>>>
109109

110110
interface Behavior<B extends BehaviorOption> {
111111
/** 类似于mixins和traits的组件间代码复用机制,参见 [behaviors](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/behaviors.html) */

0 commit comments

Comments
 (0)