From c54c382c66684f32d5f4737e4b79f71f0f8fddd6 Mon Sep 17 00:00:00 2001 From: lvzl <627417163@qq.com> Date: Mon, 2 Sep 2024 14:59:51 +0800 Subject: [PATCH 1/6] feat: supports the issues/331 mentioned in points 2, 3, 4 --- test/component.test.ts | 19 ++++--------------- test/issue.test.ts | 4 +++- types/wx/lib.wx.component.d.ts | 13 +++++++++---- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/test/component.test.ts b/test/component.test.ts index 794b0e0..fb5530b 100644 --- a/test/component.test.ts +++ b/test/component.test.ts @@ -29,7 +29,7 @@ Component({ max: { type: Number, value: 0, - observer(newVal, oldVal) { + observer(newVal: number, oldVal: number) { expectType(newVal) expectType(oldVal) expectType(this.onMyButtonTap()) @@ -120,17 +120,6 @@ Component({ }, }) -expectError( - Component({ - custom: 1, - methods: { - f() { - this.custom - }, - }, - }), -) - interface Config { a: number } @@ -178,7 +167,7 @@ Component({ expectType(this.data.n2) expectType(this.data.s) expectType(this.data.a) - expectType(this.data.a2) + expectType(this.data.a2) expectType(this.data.b) expectType>(this.data.o) expectType(this.data.a[0]) @@ -218,7 +207,7 @@ Component({ expectType(this.data.n2) expectType(this.data.s) expectType(this.data.a) - expectType(this.data.a2) + expectType(this.data.a2) expectType(this.data.b) expectType>(this.data.o) expectType>(this.data.o2) @@ -243,7 +232,7 @@ Component({ methods: { f() { expectType(this.data.n) - expectType(this.data.a) + expectType(this.data.a) }, }, }) diff --git a/test/issue.test.ts b/test/issue.test.ts index 1fb6a12..76134ad 100644 --- a/test/issue.test.ts +++ b/test/issue.test.ts @@ -397,7 +397,9 @@ import WX = WechatMiniprogram return this.data.foo }, test() { - expectType>(this.data.bar) + expectType<{ + skuNum: number + }>(this.data.bar) expectType(this.getDataOrPoperty()) }, } diff --git a/types/wx/lib.wx.component.d.ts b/types/wx/lib.wx.component.d.ts index 1b0f4da..4255d27 100644 --- a/types/wx/lib.wx.component.d.ts +++ b/types/wx/lib.wx.component.d.ts @@ -55,6 +55,8 @@ declare namespace WechatMiniprogram.Component { Partial> & Partial & Partial & + // 有很大几率会在 this.xxx 上使用一些暂存的变量,应该像Page一样支持传入自定义属性 + Partial & ThisType< Instance< TData, @@ -67,8 +69,10 @@ declare namespace WechatMiniprogram.Component { interface Constructor { < TData extends DataOption, - TProperty extends PropertyOption, - TMethod extends MethodOption, + // 给泛型默认值,避免出现当组件无 properties 选项时 + // 当xxx未在 data 中声明,this.data.xxx 为 any 的问题。 + TProperty extends PropertyOption = {}, + TMethod extends MethodOption = {}, TCustomInstanceProperty extends IAnyObject = {}, TIsPage extends boolean = false >( @@ -151,8 +155,9 @@ declare namespace WechatMiniprogram.Component { type PropertyToData = T extends ShortProperty ? ValueType : FullPropertyToData> - type FullPropertyToData = ValueType - // type FullPropertyToData = unknown extends T['value'] ? ValueType : T['value'] + type ArrayOrObject = ArrayConstructor | ObjectConstructor + // 支持 Array、Object 的 property 通过 value as ValueType 的方式明确 property 的类型 + type FullPropertyToData = T['type'] extends ArrayOrObject ? unknown extends T['value'] ? ValueType : T['value'] : ValueType type PropertyOptionToData

= { [name in keyof P]: PropertyToData } From 78a21eb92d0620efa15acb1553529957d789f1fe Mon Sep 17 00:00:00 2001 From: lvzl <627417163@qq.com> Date: Mon, 2 Sep 2024 15:33:29 +0800 Subject: [PATCH 2/6] feat: issues/331 2,3,4 test case --- test/issue.test.ts | 47 +++++++++++++++++++++++++++++++++- types/wx/lib.wx.component.d.ts | 2 +- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/test/issue.test.ts b/test/issue.test.ts index 76134ad..f218a77 100644 --- a/test/issue.test.ts +++ b/test/issue.test.ts @@ -1,4 +1,4 @@ -import { expectType } from 'tsd' +import { expectError, expectType } from 'tsd' // https://github.com/wechat-miniprogram/api-typings/issues/11 expectType(wx.env.USER_DATA_PATH) @@ -443,3 +443,48 @@ import WX = WechatMiniprogram expectType(offscreenCanvas.height) expectType(offscreenCanvas.width) } + +// https://github.com/wechat-miniprogram/api-typings/issues/332 +{ + Component({ + data: { + a: '' + }, + methods: { + test() { + expectError(this.data.xxx) + }, + } + }) +} + +{ + interface Foo { + f: string + } + + Component({ + _timer: '', + properties: { + bar: { + type: Object, + value: { f: '' } as Foo, + observer () {} + }, + foo: { + type: Array, + value: [] as Foo[], + } + }, + methods: { + getData() { + return this.data.foo + }, + test() { + expectType(this.data.bar) + expectType(this.getData()) + expectType(this._timer) + }, + } + }) +} \ 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 4255d27..c56fd8d 100644 --- a/types/wx/lib.wx.component.d.ts +++ b/types/wx/lib.wx.component.d.ts @@ -70,7 +70,7 @@ declare namespace WechatMiniprogram.Component { < TData extends DataOption, // 给泛型默认值,避免出现当组件无 properties 选项时 - // 当xxx未在 data 中声明,this.data.xxx 为 any 的问题。 + // 当xxx未在 data 中声明,this.data.xxx 为 any 且不报 TS2339 error 的问题。 TProperty extends PropertyOption = {}, TMethod extends MethodOption = {}, TCustomInstanceProperty extends IAnyObject = {}, From 8a058328ec5484d18fb56771016c163696e17889 Mon Sep 17 00:00:00 2001 From: lvzl <627417163@qq.com> Date: Tue, 3 Sep 2024 15:50:28 +0800 Subject: [PATCH 3/6] feat: comments resolve --- test/component.test.ts | 2 +- types/wx/lib.wx.component.d.ts | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/test/component.test.ts b/test/component.test.ts index fb5530b..7317097 100644 --- a/test/component.test.ts +++ b/test/component.test.ts @@ -29,7 +29,7 @@ Component({ max: { type: Number, value: 0, - observer(newVal: number, oldVal: number) { + observer(newVal, oldVal) { expectType(newVal) expectType(oldVal) expectType(this.onMyButtonTap()) diff --git a/types/wx/lib.wx.component.d.ts b/types/wx/lib.wx.component.d.ts index c56fd8d..321841a 100644 --- a/types/wx/lib.wx.component.d.ts +++ b/types/wx/lib.wx.component.d.ts @@ -21,6 +21,7 @@ SOFTWARE. ***************************************************************************** */ declare namespace WechatMiniprogram.Component { + type FilterUnknownProperty = string extends keyof TProperty ? {} : TProperty type Instance< TData extends DataOption, TProperty extends PropertyOption, @@ -33,9 +34,9 @@ declare namespace WechatMiniprogram.Component { (TIsPage extends true ? Page.ILifetime : {}) & TCustomInstanceProperty & { /** 组件数据,**包括内部数据和属性值** */ - data: TData & PropertyOptionToData + data: TData & PropertyOptionToData> /** 组件数据,**包括内部数据和属性值**(与 `data` 一致) */ - properties: TData & PropertyOptionToData + properties: TData & PropertyOptionToData> } type TrivialInstance = Instance< IAnyObject, @@ -69,10 +70,8 @@ declare namespace WechatMiniprogram.Component { interface Constructor { < TData extends DataOption, - // 给泛型默认值,避免出现当组件无 properties 选项时 - // 当xxx未在 data 中声明,this.data.xxx 为 any 且不报 TS2339 error 的问题。 - TProperty extends PropertyOption = {}, - TMethod extends MethodOption = {}, + TProperty extends PropertyOption, + TMethod extends MethodOption, TCustomInstanceProperty extends IAnyObject = {}, TIsPage extends boolean = false >( From 5b43e5428a071b880069281348d3011f94323138 Mon Sep 17 00:00:00 2001 From: lvzl <627417163@qq.com> Date: Tue, 3 Sep 2024 16:08:34 +0800 Subject: [PATCH 4/6] revert: TCustomInstanceProperty --- types/wx/lib.wx.component.d.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/types/wx/lib.wx.component.d.ts b/types/wx/lib.wx.component.d.ts index 321841a..57dfbf5 100644 --- a/types/wx/lib.wx.component.d.ts +++ b/types/wx/lib.wx.component.d.ts @@ -56,8 +56,6 @@ declare namespace WechatMiniprogram.Component { Partial> & Partial & Partial & - // 有很大几率会在 this.xxx 上使用一些暂存的变量,应该像Page一样支持传入自定义属性 - Partial & ThisType< Instance< TData, From b9aacb6106be2761e755d3e37e48e9a6b1ab87d3 Mon Sep 17 00:00:00 2001 From: lvzl <627417163@qq.com> Date: Tue, 3 Sep 2024 16:08:58 +0800 Subject: [PATCH 5/6] revert: TCustomInstanceProperty --- test/component.test.ts | 11 +++++++++++ test/issue.test.ts | 2 -- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/test/component.test.ts b/test/component.test.ts index 7317097..de6fa11 100644 --- a/test/component.test.ts +++ b/test/component.test.ts @@ -120,6 +120,17 @@ Component({ }, }) +expectError( + Component({ + custom: 1, + methods: { + f() { + this.custom + }, + }, + }), +) + interface Config { a: number } diff --git a/test/issue.test.ts b/test/issue.test.ts index f218a77..0dfd294 100644 --- a/test/issue.test.ts +++ b/test/issue.test.ts @@ -464,7 +464,6 @@ import WX = WechatMiniprogram } Component({ - _timer: '', properties: { bar: { type: Object, @@ -483,7 +482,6 @@ import WX = WechatMiniprogram test() { expectType(this.data.bar) expectType(this.getData()) - expectType(this._timer) }, } }) From 7002da60f841e2e8c88296b155fa4c23f084d685 Mon Sep 17 00:00:00 2001 From: lvzl <627417163@qq.com> Date: Tue, 3 Sep 2024 19:00:51 +0800 Subject: [PATCH 6/6] chore: remove useless comments --- types/wx/lib.wx.component.d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/types/wx/lib.wx.component.d.ts b/types/wx/lib.wx.component.d.ts index 57dfbf5..86abe30 100644 --- a/types/wx/lib.wx.component.d.ts +++ b/types/wx/lib.wx.component.d.ts @@ -153,7 +153,6 @@ declare namespace WechatMiniprogram.Component { ? ValueType : FullPropertyToData> type ArrayOrObject = ArrayConstructor | ObjectConstructor - // 支持 Array、Object 的 property 通过 value as ValueType 的方式明确 property 的类型 type FullPropertyToData = T['type'] extends ArrayOrObject ? unknown extends T['value'] ? ValueType : T['value'] : ValueType type PropertyOptionToData

= { [name in keyof P]: PropertyToData