-
Notifications
You must be signed in to change notification settings - Fork 112
feat: behavior ts type support #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
40e11c6
14bb344
3a100d0
56d3e56
f78974b
3af2210
5e6a22b
57d4c82
e940cb8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,41 +26,50 @@ declare namespace WechatMiniprogram.Component { | |
TData extends DataOption, | ||
TProperty extends PropertyOption, | ||
TMethod extends Partial<MethodOption>, | ||
TBehavior extends BehaviorOption = [], | ||
TCustomInstanceProperty extends IAnyObject = {}, | ||
TIsPage extends boolean = false | ||
> = InstanceProperties & | ||
InstanceMethods<TData> & | ||
TMethod & | ||
MixinMethods<TBehavior> & | ||
(TIsPage extends true ? Page.ILifetime : {}) & | ||
TCustomInstanceProperty & { | ||
/** 组件数据,**包括内部数据和属性值** */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个注释被改没了,可以补回来一下 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
data: TData & PropertyOptionToData<FilterUnknownProperty<TProperty>> | ||
Omit<TCustomInstanceProperty, 'properties' | 'methods' | 'data'> & { | ||
data: TData & MixinData<TBehavior> & | ||
MixinProperties<TBehavior> & PropertyOptionToData<FilterUnknownProperty<TProperty>> | ||
/** 组件数据,**包括内部数据和属性值**(与 `data` 一致) */ | ||
properties: TData & PropertyOptionToData<FilterUnknownProperty<TProperty>> | ||
properties: TData & MixinData<TBehavior> & | ||
MixinProperties<TBehavior> & PropertyOptionToData<FilterUnknownProperty<TProperty>> | ||
} | ||
|
||
type IAnyArray = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个或许应该是 可以挪到 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
type TrivialInstance = Instance< | ||
IAnyObject, | ||
IAnyObject, | ||
IAnyObject, | ||
IAnyArray, | ||
IAnyObject | ||
> | ||
type TrivialOption = Options<IAnyObject, IAnyObject, IAnyObject, IAnyObject> | ||
type TrivialOption = Options<IAnyObject, IAnyObject, IAnyObject, IAnyArray, IAnyObject> | ||
type Options< | ||
TData extends DataOption, | ||
TProperty extends PropertyOption, | ||
TMethod extends MethodOption, | ||
TBehavior extends BehaviorOption = [], | ||
TCustomInstanceProperty extends IAnyObject = {}, | ||
TIsPage extends boolean = false | ||
> = Partial<Data<TData>> & | ||
Partial<Property<TProperty>> & | ||
Partial<Method<TMethod, TIsPage>> & | ||
Partial<Behavior<TBehavior>> & | ||
Partial<OtherOption> & | ||
Partial<Lifetimes> & | ||
ThisType< | ||
Instance< | ||
TData, | ||
TProperty, | ||
TMethod, | ||
TBehavior, | ||
TCustomInstanceProperty, | ||
TIsPage | ||
> | ||
|
@@ -70,13 +79,15 @@ declare namespace WechatMiniprogram.Component { | |
TData extends DataOption, | ||
TProperty extends PropertyOption, | ||
TMethod extends MethodOption, | ||
TBehavior extends BehaviorOption = [], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个默认值是有必要的不,能不能和上面三个保持一致(都有默认值或者都没有) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (上面的 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
TCustomInstanceProperty extends IAnyObject = {}, | ||
TIsPage extends boolean = false | ||
>( | ||
options: Options< | ||
TData, | ||
TProperty, | ||
TMethod, | ||
TBehavior, | ||
TCustomInstanceProperty, | ||
TIsPage | ||
> | ||
|
@@ -86,6 +97,22 @@ declare namespace WechatMiniprogram.Component { | |
type PropertyOption = Record<string, AllProperty> | ||
type MethodOption = Record<string, Function> | ||
|
||
type BehaviorOption = Behavior.BehaviorIdentifier[] | ||
type ExtractBehaviorType<T> = T extends { BehaviorType?: infer B } ? B : never | ||
type ExtractData<T> = T extends { data: infer D } ? D : never | ||
type ExtractProperties<T, TIsBehavior extends boolean = false> = T extends { properties: infer P } ? | ||
TIsBehavior extends true ? P : PropertyOptionToData<P extends PropertyOption ? P : {}> : never | ||
type ExtractMethods<T> = T extends { methods: infer M } ? M : never | ||
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never | ||
type MixinData<T extends any[]> = UnionToIntersection<ExtractData<ExtractBehaviorType<T[number]>>> | ||
type MixinProperties<T extends any[], TIsBehavior extends boolean = false> = UnionToIntersection<ExtractProperties<ExtractBehaviorType<T[number]>, TIsBehavior>> | ||
type MixinMethods<T extends any[]> = UnionToIntersection<ExtractMethods<ExtractBehaviorType<T[number]>>> | ||
|
||
interface Behavior<B extends BehaviorOption> { | ||
/** 类似于mixins和traits的组件间代码复用机制,参见 [behaviors](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/behaviors.html) */ | ||
behaviors?: B | ||
} | ||
|
||
interface Data<D extends DataOption> { | ||
/** 组件的内部数据,和 `properties` 一同用于组件的模板渲染 */ | ||
data?: D | ||
|
@@ -524,8 +551,6 @@ declare namespace WechatMiniprogram.Component { | |
} | ||
|
||
interface OtherOption { | ||
/** 类似于mixins和traits的组件间代码复用机制,参见 [behaviors](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/behaviors.html) */ | ||
behaviors: Behavior.BehaviorIdentifier[] | ||
/** | ||
* 组件数据字段监听器,用于监听 properties 和 data 的变化,参见 [数据监听器](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/observer.html) | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个
RealBehaviorType
可能还是直接交叉进BehaviorIdentifier
好一点,比较方便外面引用,比如或者
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done