From b8471f2e25dd64222fecd52bf015aaa261c40c07 Mon Sep 17 00:00:00 2001 From: Yang Mingshan Date: Sun, 6 Oct 2024 22:54:23 +0800 Subject: [PATCH] feat: prioritize inferring property type from value --- test/issue.test.ts | 28 ++++++++++++++++++++++++++-- types/wx/lib.wx.component.d.ts | 3 +-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/test/issue.test.ts b/test/issue.test.ts index 9390d0c..b2b62e1 100644 --- a/test/issue.test.ts +++ b/test/issue.test.ts @@ -467,6 +467,22 @@ import WX = WechatMiniprogram Component({ properties: { + any: { + type: null, + value: '', + }, + num: { + type: Number, + value: 0 as 0 | 1, + }, + str: { + type: String, + value: 'A' as 'A' | 'B', + }, + bool: { + type: Boolean, + value: false, + }, bar: { type: Object, value: { f: '' } as Foo, @@ -475,16 +491,24 @@ import WX = WechatMiniprogram foo: { type: Array, value: [] as Foo[], - } + }, + noValue: { + type: String, + }, }, methods: { getData() { return this.data.foo }, test() { + expectType(this.data.any) + expectType<0 | 1>(this.data.num) + expectType<'A' | 'B'>(this.data.str) + expectType(this.data.bool) expectType(this.data.bar) expectType(this.properties.bar) expectType(this.getData()) + expectType(this.data.noValue) }, } }) @@ -634,4 +658,4 @@ import WX = WechatMiniprogram }, } }) -} \ No newline at end of file +} diff --git a/types/wx/lib.wx.component.d.ts b/types/wx/lib.wx.component.d.ts index 3753624..cb4e219 100644 --- a/types/wx/lib.wx.component.d.ts +++ b/types/wx/lib.wx.component.d.ts @@ -180,8 +180,7 @@ declare namespace WechatMiniprogram.Component { type PropertyToData = T extends ShortProperty ? ValueType : FullPropertyToData> - type ArrayOrObject = ArrayConstructor | ObjectConstructor - type FullPropertyToData = T['type'] extends ArrayOrObject ? unknown extends T['value'] ? ValueType : T['value'] : ValueType + type FullPropertyToData = T['type'] extends null | BooleanConstructor ? ValueType : unknown extends T['value'] ? ValueType : T['value'] type PropertyOptionToData

= { [name in keyof P]: PropertyToData }