Skip to content

computed 中定义的属性相互引用时丢失类型提示 #103

Description

@liuweiGL

当前存在的问题

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

image

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions