Skip to content

Commit

Permalink
feat: optimize the implementation of behavior type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
lv-z-l committed Sep 5, 2024
1 parent 3af2210 commit 5e6a22b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion test/behavior.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expectType } from 'tsd'

expectType<string & { BehaviorType: { data: {}, properties: {}, methods: {} } }>(Behavior({}))
expectType<string & {}>(Behavior({}))

Behavior({
behaviors: [],
Expand Down
2 changes: 1 addition & 1 deletion test/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expectType, expectError } from 'tsd'
expectType<string>(Component({}))

Component({
behaviors: [],
behaviors: [''],

properties: {
myProperty: {
Expand Down
9 changes: 5 additions & 4 deletions types/wx/lib.wx.behavior.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ SOFTWARE.
***************************************************************************** */

declare namespace WechatMiniprogram.Behavior {
type BehaviorIdentifier<
type RealBehaviorType<
TData extends DataOption = {},
TProperty extends PropertyOption = {},
TMethod extends MethodOption = {}
> = string & {
BehaviorType: {
> = {
[key in 'BehaviorType']?: {
data: TData
properties: TProperty
methods: TMethod
}
}
type BehaviorIdentifier = string
type Instance<
TData extends DataOption,
TProperty extends PropertyOption,
Expand Down Expand Up @@ -59,7 +60,7 @@ declare namespace WechatMiniprogram.Behavior {
TCustomInstanceProperty extends IAnyObject = Record<string, never>
>(
options: Options<TData, TProperty, TMethod, TCustomInstanceProperty>
): BehaviorIdentifier<TData, TProperty, TMethod>
): BehaviorIdentifier & RealBehaviorType<TData, TProperty, TMethod>
}

type DataOption = Component.DataOption
Expand Down
2 changes: 1 addition & 1 deletion types/wx/lib.wx.component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ declare namespace WechatMiniprogram.Component {
type MethodOption = Record<string, Function>

type BehaviorOption = Behavior.BehaviorIdentifier[]
type ExtractBehaviorType<T> = T extends { BehaviorType: infer B } ? B : never
type ExtractBehaviorType<T> = T extends { BehaviorType?: infer B } ? B : never
type ExtractData<T> = T extends { data: infer D } ? D : never
type ExtractProperties<T> = T extends { properties: infer P } ? PropertyOptionToData<P extends PropertyOption ? P : {}> : never
type ExtractMethods<T> = T extends { methods: infer M } ? M : never
Expand Down

0 comments on commit 5e6a22b

Please sign in to comment.