Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: supports the issues/331 mentioned in points 2, 3, 4 #332

Merged
merged 6 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
},
}
})
}
10 changes: 6 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,9 @@ 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
// 支持 Array、Object 的 property 通过 value as ValueType 的方式明确 property 的类型
SgLy marked this conversation as resolved.
Show resolved Hide resolved
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
Loading