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: prioritize inferring property type from value #335

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
28 changes: 26 additions & 2 deletions test/issue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -475,16 +491,24 @@ import WX = WechatMiniprogram
foo: {
type: Array,
value: [] as Foo[],
}
},
noValue: {
type: String,
},
},
methods: {
getData() {
return this.data.foo
},
test() {
expectType<any>(this.data.any)
expectType<0 | 1>(this.data.num)
expectType<'A' | 'B'>(this.data.str)
expectType<boolean>(this.data.bool)
expectType<Foo>(this.data.bar)
expectType<Foo>(this.properties.bar)
expectType<Foo[]>(this.getData())
expectType<string>(this.data.noValue)
},
}
})
Expand Down Expand Up @@ -634,4 +658,4 @@ import WX = WechatMiniprogram
},
}
})
}
}
3 changes: 1 addition & 2 deletions types/wx/lib.wx.component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ declare namespace WechatMiniprogram.Component {
type PropertyToData<T extends AllProperty> = T extends ShortProperty
? ValueType<T>
: FullPropertyToData<Exclude<T, ShortProperty>>
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 FullPropertyToData<T extends AllFullProperty> = T['type'] extends null | BooleanConstructor ? ValueType<T['type']> : unknown extends T['value'] ? ValueType<T['type']> : T['value']
type PropertyOptionToData<P extends PropertyOption> = {
[name in keyof P]: PropertyToData<P[name]>
}
Expand Down
Loading