Skip to content

Commit

Permalink
Merge pull request #332 from lv-z-l/master
Browse files Browse the repository at this point in the history
feat: supports the issues/331 mentioned in points 2, 3, 4
  • Loading branch information
SgLy authored Sep 3, 2024
2 parents 6a7f4f9 + 7002da6 commit 0a76c78
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
6 changes: 3 additions & 3 deletions test/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ Component({
expectType<number>(this.data.n2)
expectType<string>(this.data.s)
expectType<any[]>(this.data.a)
expectType<any[]>(this.data.a2)
expectType<number[]>(this.data.a2)
expectType<boolean>(this.data.b)
expectType<Record<string, any>>(this.data.o)
expectType<any>(this.data.a[0])
Expand Down Expand Up @@ -218,7 +218,7 @@ Component({
expectType<number>(this.data.n2)
expectType<string>(this.data.s)
expectType<any[]>(this.data.a)
expectType<any[]>(this.data.a2)
expectType<number[]>(this.data.a2)
expectType<boolean>(this.data.b)
expectType<Record<string, any>>(this.data.o)
expectType<Record<string, any>>(this.data.o2)
Expand All @@ -243,7 +243,7 @@ Component({
methods: {
f() {
expectType<number>(this.data.n)
expectType<any[]>(this.data.a)
expectType<number[]>(this.data.a)
},
},
})
Expand Down
49 changes: 47 additions & 2 deletions test/issue.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expectType } from 'tsd'
import { expectError, expectType } from 'tsd'

// https://github.com/wechat-miniprogram/api-typings/issues/11
expectType<string>(wx.env.USER_DATA_PATH)
Expand Down Expand Up @@ -397,7 +397,9 @@ import WX = WechatMiniprogram
return this.data.foo
},
test() {
expectType<Record<string, any>>(this.data.bar)
expectType<{
skuNum: number
}>(this.data.bar)
expectType<number>(this.getDataOrPoperty())
},
}
Expand Down Expand Up @@ -441,3 +443,46 @@ import WX = WechatMiniprogram
expectType<number>(offscreenCanvas.height)
expectType<number>(offscreenCanvas.width)
}

// https://github.com/wechat-miniprogram/api-typings/issues/332
{
Component({
data: {
a: ''
},
methods: {
test() {
expectError<any>(this.data.xxx)
},
}
})
}

{
interface Foo {
f: string
}

Component({
properties: {
bar: {
type: Object,
value: { f: '' } as Foo,
observer () {}
},
foo: {
type: Array,
value: [] as Foo[],
}
},
methods: {
getData() {
return this.data.foo
},
test() {
expectType<Foo>(this.data.bar)
expectType<Foo[]>(this.getData())
},
}
})
}
9 changes: 5 additions & 4 deletions types/wx/lib.wx.component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ SOFTWARE.
***************************************************************************** */

declare namespace WechatMiniprogram.Component {
type FilterUnknownProperty<TProperty> = string extends keyof TProperty ? {} : TProperty
type Instance<
TData extends DataOption,
TProperty extends PropertyOption,
Expand All @@ -33,9 +34,9 @@ declare namespace WechatMiniprogram.Component {
(TIsPage extends true ? Page.ILifetime : {}) &
TCustomInstanceProperty & {
/** 组件数据,**包括内部数据和属性值** */
data: TData & PropertyOptionToData<TProperty>
data: TData & PropertyOptionToData<FilterUnknownProperty<TProperty>>
/** 组件数据,**包括内部数据和属性值**(与 `data` 一致) */
properties: TData & PropertyOptionToData<TProperty>
properties: TData & PropertyOptionToData<FilterUnknownProperty<TProperty>>
}
type TrivialInstance = Instance<
IAnyObject,
Expand Down Expand Up @@ -151,8 +152,8 @@ declare namespace WechatMiniprogram.Component {
type PropertyToData<T extends AllProperty> = T extends ShortProperty
? ValueType<T>
: FullPropertyToData<Exclude<T, ShortProperty>>
type FullPropertyToData<T extends AllFullProperty> = ValueType<T['type']>
// type FullPropertyToData<T extends AllFullProperty> = unknown extends T['value'] ? ValueType<T['type']> : T['value']
type ArrayOrObject = ArrayConstructor | ObjectConstructor
type FullPropertyToData<T extends AllFullProperty> = T['type'] extends ArrayOrObject ? unknown extends T['value'] ? ValueType<T['type']> : T['value'] : ValueType<T['type']>
type PropertyOptionToData<P extends PropertyOption> = {
[name in keyof P]: PropertyToData<P[name]>
}
Expand Down

0 comments on commit 0a76c78

Please sign in to comment.