Skip to content

fix(component): use both type and optionalTypes to infer property type #98

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

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
4 changes: 2 additions & 2 deletions test/behavior.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Behavior({
type: Number,
value: 0,
observer(newVal, oldVal) {
expectType<number>(newVal)
expectType<string>(oldVal.toExponential())
expectType<any>(newVal)
expectType<any>(oldVal.toExponential())
},
},
},
Expand Down
7 changes: 4 additions & 3 deletions test/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ Component({
},
max: {
type: Number,
optionalTypes: [String],
value: 0,
observer(newVal, oldVal) {
expectType<number>(newVal)
expectType<number>(oldVal)
expectType<any>(newVal)
expectType<any>(oldVal)
expectType<void>(this.onMyButtonTap())
expectType<number>(this.data.max)
expectType<number | string>(this.data.max)
},
},
lastLeaf: {
Expand Down
51 changes: 23 additions & 28 deletions types/wx/lib.wx.component.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/*! *****************************************************************************
Copyright (c) 2020 Tencent, Inc. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
***************************************************************************** */

Expand Down Expand Up @@ -97,12 +97,12 @@ declare namespace WechatMiniprogram {
observer?:
| string
| ((
newVal: ValueType<T>,
oldVal: ValueType<T>,
newVal: any,
oldVal: any,
changedPath: Array<string | number>,
) => void)
/** 属性的类型(可以指定多个) */
optionalTypes?: ShortProperty[]
optionalTypes?: PropertyType[]
}
type AllFullProperty =
| FullProperty<StringConstructor>
Expand All @@ -111,20 +111,15 @@ declare namespace WechatMiniprogram {
| FullProperty<ArrayConstructor>
| FullProperty<ObjectConstructor>
| FullProperty<null>
type ShortProperty =
| StringConstructor
| NumberConstructor
| BooleanConstructor
| ArrayConstructor
| ObjectConstructor
| null
type AllProperty = AllFullProperty | ShortProperty
type PropertyToData<T extends AllProperty> = T extends ShortProperty
type AllProperty = AllFullProperty | PropertyType
type PropertyToData<T extends AllProperty> = T extends PropertyType
? ValueType<T>
: FullPropertyToData<Exclude<T, ShortProperty>>
type FullPropertyToData<T extends AllFullProperty> = ValueType<
T['type']
>
: FullPropertyToData<Exclude<T, PropertyType>>
type FullPropertyToData<T extends AllFullProperty> =
T['optionalTypes'] extends OptionalTypes<infer V>
? ValueType<V | T['type']>
: ValueType<T['type']>
type OptionalTypes<T extends PropertyType> = T[]
type PropertyOptionToData<P extends PropertyOption> = {
[name in keyof P]: PropertyToData<P[name]>
}
Expand Down