computed
computed copied to clipboard
computed 中定义的属性相互引用时丢失类型提示
当前存在的问题
ComponentWithComputed({
computed: {
a(data){
return true
},
b(data){
return data.a
}
}
})
键入 data.
之后并没有 a 属性
原因
当前泛型反推的是整个 TComputed
类型,而 TComputed
的值为 Record<string,xxx>,所以 computed
属性的 key 类型丢失了。
解决办法
把 TCompouted
改为 TComputedKey
反推定义的 key 值:
export const ComponentWithComputed = _ComponentWithComputed as <
TData extends WechatMiniprogram.Component.DataOption,
TProperty extends WechatMiniprogram.Component.PropertyOption,
TMethod extends WechatMiniprogram.Component.MethodOption,
TWatch extends Record<string, (...args: any[]) => void>,
TBehavior extends WechatMiniprogram.Component.BehaviorOption,
TComputedKey extends string,
TCustomInstanceProperty extends
WechatMiniprogram.IAnyObject = WechatMiniprogram.IAnyObject
>(
options: ComputedOptions<
TData,
TProperty,
TMethod,
TWatch,
TBehavior,
TComputedKey,
Record<
TComputedKey,
(
data: TData & {
[key in keyof TProperty]: TProperty[key] extends null
? any
: TProperty[key]
} & { [key in TComputedKey] }
) => any
>,
TCustomInstanceProperty
>
) => string