Skip to content

Commit f7b1ba1

Browse files
committed
feat: support component type signatures for glass-easel-analyzer
1 parent 6330472 commit f7b1ba1

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

test/component.test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { expectType, expectError, expectNotAssignable } from 'tsd'
1+
import { expectType, expectError, expectNotAssignable, expectAssignable } from 'tsd'
22

3-
expectType<string>(Component({}))
3+
expectAssignable<string>(Component({}))
44

55
Component({
66
behaviors: [''],
@@ -511,3 +511,35 @@ Component<{}, {}, { fn(): void }, []>({
511511
})
512512
}
513513

514+
{
515+
const def = Component({
516+
properties: {
517+
a: Boolean,
518+
},
519+
data: {
520+
b: 1,
521+
},
522+
methods: {
523+
c() {}
524+
},
525+
})
526+
type FieldTypes = (typeof def)['_$fieldTypes']
527+
expectType<FieldTypes['propertyValues']['a']>(false as boolean)
528+
expectType<FieldTypes['dataWithProperties']['a']>(false as boolean)
529+
expectType<FieldTypes['dataWithProperties']['b']>(1 as number)
530+
expectType<FieldTypes['methods']['c']>(() => {})
531+
}
532+
533+
{
534+
type CustomProperties = {
535+
customProp: string
536+
}
537+
Component<{}, {}, {}, [], CustomProperties>({
538+
lifetimes: {
539+
created() {
540+
this.customProp = 'customProp'
541+
}
542+
}
543+
})
544+
}
545+

test/page.test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { expectType, expectError, expectNotAssignable } from 'tsd'
1+
import { expectType, expectError, expectNotAssignable, expectAssignable } from 'tsd'
22

3-
expectType<void>(Page({}))
3+
expectAssignable<void>(Page({}))
44

55
expectType<Record<string, any>>(getCurrentPages()[0].data)
66

@@ -241,4 +241,16 @@ Page({
241241
onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent {
242242
return { title: this.data.a, imageUrl: '', path: '' }
243243
},
244-
})
244+
})
245+
246+
{
247+
const def = Page({
248+
data: {
249+
b: 1,
250+
},
251+
c() {},
252+
})
253+
type FieldTypes = (typeof def)['_$fieldTypes']
254+
expectType<FieldTypes['dataWithProperties']['b']>(1 as number)
255+
expectType<FieldTypes['methods']['c']>(() => {})
256+
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ declare namespace WechatMiniprogram.Component {
9494
TCustomInstanceProperty,
9595
TIsPage
9696
>
97-
): string
97+
): string & ComponentTypeSignature<TData, TProperty, TMethod>
9898
}
9999
type DataOption = Record<string, any>
100100
type PropertyOption = Record<string, AllProperty>
@@ -111,6 +111,19 @@ declare namespace WechatMiniprogram.Component {
111111
type MixinProperties<T extends any[], TIsBehavior extends boolean = false> = UnionToIntersection<ExtractProperties<ExtractBehaviorType<T[number]>, TIsBehavior>>
112112
type MixinMethods<T extends any[]> = UnionToIntersection<ExtractMethods<ExtractBehaviorType<T[number]>>>
113113

114+
/** 用于辅助识别组件类型的虚拟字段(供 glass-easel-analyzer 等外部模块使用) */
115+
class ComponentTypeSignature<
116+
TData extends DataOption,
117+
TProperty extends PropertyOption,
118+
TMethod extends MethodOption,
119+
> {
120+
protected readonly _$fieldTypes: {
121+
propertyValues: PropertyOptionToData<TProperty>
122+
dataWithProperties: TData & PropertyOptionToData<TProperty>
123+
methods: TMethod
124+
}
125+
}
126+
114127
interface Behavior<B extends BehaviorOption> {
115128
/** 类似于mixins和traits的组件间代码复用机制,参见 [behaviors](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/behaviors.html) */
116129
behaviors?: B

types/wx/lib.wx.page.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ declare namespace WechatMiniprogram.Page {
4242
interface Constructor {
4343
<TData extends DataOption, TCustom extends CustomOption>(
4444
options: Options<TData, TCustom>
45-
): void
45+
): void & Component.ComponentTypeSignature<
46+
TData,
47+
Record<never, never>,
48+
{ [K in keyof TCustom as TCustom[K] extends Function ? K : never]: TCustom[K] }
49+
>
4650
}
4751
interface ILifetime {
4852
/** 生命周期回调—监听页面加载

0 commit comments

Comments
 (0)