Skip to content

Conversation

@pointhalo
Copy link
Collaborator

@pointhalo pointhalo commented Mar 4, 2025

中文模板 / Chinese Template

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Test Case
  • TypeScript definition update
  • Document improve
  • CI/CD improve
  • Branch sync
  • Other, please describe:

PR description

  • 原先的写法,对于 array[0]、array.1 这种写法无法正确推断类型。只要FormApi的泛型中带上了 array类型,类型提示就会丢失。。
  • 改了写法,兼容上述场景。并保持其他场景不变
  • 升级项目依赖的 ts版本,对一些不重要/未来会废弃的报错ignore,对一些不合理的type check改正,在 ts 5.7.2 中自测通过
    image
    image

Changelog

🇨🇳 Chinese

  • Chore: 对 Form 、FormApi 泛型嵌套路径推断做优化,支持数组类型下标推断。解决原写法 array 类型回导致类型提示丢失的问题
  • Chore: useFieldApi 支持传入泛型, 以 useFieldApi<FormData, 'xxx'>('xxx') 的方式使用

🇺🇸 English

    • Chore: Optimize generic nested path inference for Form and FormApi, support subscript inference for array types. Solve the problem that the original writing style of array types leads to the loss of type hints.

Checklist

  • Test or no need
  • Document or no need
  • Changelog or no need

Other

  • Skip Changelog

Additional information

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 4, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 81f1818:

Sandbox Source
pr-story Configuration

@cypress
Copy link

cypress bot commented Mar 4, 2025

semi-design    Run #3129

Run Properties:  status check failed Failed #3129  •  git commit 12ae483df8 ℹ️: Merge 81f1818ee590df80447d5bc12f7ac6bce7c30544 into 6d39de510b0c2b667d761d90622c...
Project semi-design
Branch Review fix-fieldPath
Run status status check failed Failed #3129
Run duration 08m 08s
Commit git commit 12ae483df8 ℹ️: Merge 81f1818ee590df80447d5bc12f7ac6bce7c30544 into 6d39de510b0c2b667d761d90622c...
Committer pointhalo
View all properties for this run ↗︎

Test results
Tests that failed  Failures 1
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 11
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 278
⚠️ You've recorded test results over your free plan limit.
Upgrade your plan to view test results.
View all changes introduced in this branch ↗︎

Tests for review

@pointhalo pointhalo changed the base branch from main to release March 5, 2025 03:23
@pointhalo
Copy link
Collaborator Author

一些过程记录

  • alpha.6 版本尝试将 FieldPath中的 Date、Set、Map 类型判断抽取,简化嵌套写法。但发现会影响类型推断。导致部分用例无法通过,在 alpha.10 中将其去除。
    image

20250311195139_rec_

image

解读
TypeScript 使用 extends 来进行条件类型推导时,T extends SomeType 的逻辑匹配不仅仅是简单的 " 类型是否匹配”,还要考虑联合类型 (|) 的分布式特性。
代码中,FieldPath 条件分支的核心逻辑在这里:
Exclude<T[K], undefined> extends Date ? ${K}: ...
这是一个条件类型,如果字段 T[K] 是 Date 类型,就满足条件,返回对应的路径。这种情况下,分支是单一类型匹配 (extends Date) 的,TypeScript 能够很好地理解并应用它。
为什么扩展到 Date | Set | Map 会导致问题?
当将条件扩展为: Exclude<T[K], undefined> extends Date | Set | Map
引入了一个更复杂的匹配条件。此时,TypeScript 会考虑联合类型的分布式特性,而 Date | Set | Map 是一个联合类型。分布式特性会将 T[K] 的每个可能的类型与联合类型中的每个成员逐一比较,再决定是否满足条件。
主要问题出在 TypeScript 对于复杂条件类型推导的处理上:
联合类型分布式计算:
假设 T[K] 是 Set | undefined。
Exclude<T[K], undefined> 变成 Set。
TypeScript 会将 Set 与 Date | Set | Map 逐个比较:
Set 是否是 Date?不是。
Set 是否是 Set?是。
Set 是否是 Map?不是。
由于这个过程相对复杂,TypeScript 的条件推断可能无法准确区分路径,尤其是在嵌套结构中。
泛型参数的宽泛推导性:
如果 T[K] 是联合类型或复杂类型,TypeScript 不容易推导出明确的路径类型。
特别是当嵌套对象和数组的层级增加时,条件类型的嵌套匹配可能失效。

@pointhalo
Copy link
Collaborator Author

alpha.10 版本效果
20250311200142_rec_

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants