Skip to content

Commit 5e6a22b

Browse files
committed
feat: optimize the implementation of behavior type inference
1 parent 3af2210 commit 5e6a22b

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

test/behavior.test.ts

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

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

55
Behavior({
66
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: [],
6+
behaviors: [''],
77

88
properties: {
99
myProperty: {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ SOFTWARE.
2121
***************************************************************************** */
2222

2323
declare namespace WechatMiniprogram.Behavior {
24-
type BehaviorIdentifier<
24+
type RealBehaviorType<
2525
TData extends DataOption = {},
2626
TProperty extends PropertyOption = {},
2727
TMethod extends MethodOption = {}
28-
> = string & {
29-
BehaviorType: {
28+
> = {
29+
[key in 'BehaviorType']?: {
3030
data: TData
3131
properties: TProperty
3232
methods: TMethod
3333
}
3434
}
35+
type BehaviorIdentifier = string
3536
type Instance<
3637
TData extends DataOption,
3738
TProperty extends PropertyOption,
@@ -59,7 +60,7 @@ declare namespace WechatMiniprogram.Behavior {
5960
TCustomInstanceProperty extends IAnyObject = Record<string, never>
6061
>(
6162
options: Options<TData, TProperty, TMethod, TCustomInstanceProperty>
62-
): BehaviorIdentifier<TData, TProperty, TMethod>
63+
): BehaviorIdentifier & RealBehaviorType<TData, TProperty, TMethod>
6364
}
6465

6566
type DataOption = Component.DataOption

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ declare namespace WechatMiniprogram.Component {
9898
type MethodOption = Record<string, Function>
9999

100100
type BehaviorOption = Behavior.BehaviorIdentifier[]
101-
type ExtractBehaviorType<T> = T extends { BehaviorType: infer B } ? B : never
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

0 commit comments

Comments
 (0)