wechat-miniprogram / wechat-miniprogram/computed

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

Open
#103 0 comments 0 reactions 1 assignee View on GitHub

@LastLeaf is already working on this.

Since Oct 18, 2024.

Dominant language
TypeScript
Stars
696
Forks
61
PR merge metrics
No merged PRs in 30d

Description

当前存在的问题

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.