Skip to content

Commit 74006ad

Browse files
committed
feat: glass-easel chaining api supports TypeSignature for glass-easel-analyzer
1 parent f44c40e commit 74006ad

4 files changed

Lines changed: 91 additions & 12 deletions

File tree

test/component.glass-easel.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,51 @@ import { expectType } from 'tsd'
570570
})
571571
.register()
572572
}
573+
574+
// TypeSignature: Component register() should carry field types for glass-easel-analyzer
575+
{
576+
const def = Component()
577+
.property('a', Boolean)
578+
.data(() => ({
579+
b: 1,
580+
}))
581+
.methods({
582+
c() {},
583+
})
584+
.register()
585+
type FieldTypes = (typeof def)['_$fieldTypes']
586+
expectType<NonNullable<FieldTypes>['propertyValues']['a']>(false as boolean)
587+
expectType<NonNullable<FieldTypes>['dataWithProperties']['a']>(false as boolean)
588+
expectType<NonNullable<FieldTypes>['dataWithProperties']['b']>(1 as number)
589+
expectType<NonNullable<FieldTypes>['methods']['c']>(() => {})
590+
}
591+
592+
// TypeSignature: Behavior register() should carry field types for glass-easel-analyzer
593+
{
594+
const beh = Behavior()
595+
.property('a', Number)
596+
.data(() => ({
597+
b: 'hello',
598+
}))
599+
.methods({
600+
c() { return 1 },
601+
})
602+
.register()
603+
type FieldTypes = (typeof beh)['_$behaviorFieldTypes']
604+
expectType<NonNullable<FieldTypes>['propertyValues']['a']>(1 as number)
605+
expectType<NonNullable<FieldTypes>['dataWithProperties']['a']>(1 as number)
606+
expectType<NonNullable<FieldTypes>['dataWithProperties']['b']>('hello' as string)
607+
expectType<NonNullable<FieldTypes>['methods']['c']>(() => 1)
608+
}
609+
610+
// Property value type: precise value type inference (like glass-easel)
611+
{
612+
Component()
613+
.property('n', { type: Number, value: 1 as const })
614+
.property('o', { type: Object, value: { f1: 123 } })
615+
.lifetime('attached', function () {
616+
expectType<1>(this.data.n)
617+
expectType<number>(this.data.o.f1)
618+
})
619+
.register()
620+
}

types/wx/lib.wx.api.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21814,7 +21814,7 @@ Page({
2181421814
* 衍生值 `DerivedValue`,可基于已有的 `SharedValue` 生成其它共享变量。 */
2181521815
derived(
2181621816
/** worklet 函数类型,该函数被立即执行,返回值作为 DerivedValue 的初始值。当函数内捕获的 SharedValue 类型值发生变化时,updaterWorklet 被驱动执行,返回值用于更新 DerivedValue。可类比 computed 计算属性进行理解。 */
21817-
updaterWorklet: Skyline.WorkletFunction
21817+
updaterWorklet: Skyline.WorkletFunction<any>
2181821818
): Skyline.DerivedValue<any>
2181921819
/** [SharedValue worklet.shared(any initialValue)](https://developers.weixin.qq.com/miniprogram/dev/api/ui/worklet/base/worklet.shared.html)
2182021820
*

types/wx/lib.wx.glass-easel.behavior.d.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// this rule seems buggy, unable to distinguish WechatMiniprogram.GlassEasel.Component and WechatMiniprogram.Component
2+
/* eslint-disable @typescript-eslint/no-unnecessary-qualifier */
3+
14
declare namespace WechatMiniprogram.GlassEasel.Behavior {
25
type DefinitionFilter = (
36
target: Component.TrivialDefinition,
@@ -145,14 +148,27 @@ declare namespace WechatMiniprogram.GlassEasel.Behavior {
145148
): ResolveBehaviorBuilder<this, TChainingFilter>
146149
}
147150

151+
/** 用于辅助识别 behavior 字段类型的虚拟字段(供 glass-easel-analyzer 等外部模块使用) */
152+
type TypeSignature<
153+
TData extends TypeUtils.DataList,
154+
TProperty extends TypeUtils.PropertyList,
155+
TMethod extends TypeUtils.MethodList,
156+
> = {
157+
readonly _$behaviorFieldTypes?: {
158+
propertyValues: TypeUtils.PropertyValues<TProperty>
159+
dataWithProperties: TypeUtils.DataWithPropertyValues<TData, TProperty>
160+
methods: TMethod
161+
}
162+
}
163+
148164
type Instance<
149165
TData extends TypeUtils.DataList,
150166
TProperty extends TypeUtils.PropertyList,
151167
TMethod extends TypeUtils.MethodList,
152168
TChainingFilter extends TypeUtils.ChainingFilterType,
153169
TComponentExport,
154170
TExtraThisFields extends TypeUtils.DataList = TypeUtils.Empty
155-
> = {}
171+
> = TypeSignature<TData, TProperty, TMethod>
156172

157173
type TrivialInstance = Instance<
158174
/* TData */ IAnyObject,
@@ -224,7 +240,7 @@ declare namespace WechatMiniprogram.GlassEasel.Behavior {
224240
UComponentExport,
225241
UExtraThisFields
226242
>
227-
| string
243+
| WechatMiniprogram.Behavior.Identifier
228244
): ResolveBehaviorBuilder<
229245
Builder<
230246
TPrevData,

types/wx/lib.wx.glass-easel.component.d.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,26 @@ declare namespace WechatMiniprogram.GlassEasel.Component {
384384
IAnyObject
385385
> {}
386386

387+
/** 用于辅助识别组件类型的虚拟字段(供 glass-easel-analyzer 等外部模块使用) */
388+
class TypeSignature<
389+
TData extends TypeUtils.DataList,
390+
TProperty extends TypeUtils.PropertyList,
391+
TMethod extends TypeUtils.MethodList,
392+
> {
393+
protected readonly _$fieldTypes?: {
394+
propertyValues: TypeUtils.PropertyValues<TProperty>
395+
dataWithProperties: TypeUtils.DataWithPropertyValues<TData, TProperty>
396+
methods: TMethod
397+
}
398+
}
399+
387400
type ComponentType<
388401
TData extends TypeUtils.DataList,
389402
TProperty extends TypeUtils.PropertyList,
390403
TMethod extends TypeUtils.MethodList,
391404
TComponentExport,
392405
TExtraThisFields extends TypeUtils.DataList = TypeUtils.Empty
393-
> = {}
406+
> = TypeSignature<TData, TProperty, TMethod>
394407

395408
type TrivialComponentType = ComponentType<
396409
TypeUtils.DataList,
@@ -436,14 +449,16 @@ declare namespace WechatMiniprogram.GlassEasel.Component {
436449
UComponentExport,
437450
UExtraThisFields extends TypeUtils.DataList = TypeUtils.Empty
438451
>(
439-
behavior: Behavior.Instance<
440-
UData,
441-
UProperty,
442-
UMethod,
443-
UChainingFilter,
444-
UComponentExport,
445-
UExtraThisFields
446-
>
452+
behavior:
453+
| Behavior.Instance<
454+
UData,
455+
UProperty,
456+
UMethod,
457+
UChainingFilter,
458+
UComponentExport,
459+
UExtraThisFields
460+
>
461+
| WechatMiniprogram.Behavior.Identifier
447462
): Behavior.ResolveBehaviorBuilder<
448463
Builder<
449464
TPrevData,

0 commit comments

Comments
 (0)