跳到正文
Skillpedia
网站与界面(前端)13k · #11388

narrow-react-prop-types

它能做什么

它指导 AI 智能体移除那些只为测试或故事数据而存在的可选属性。适用于希望类型严格、准确的 React 开发者。

适用场景

  • ✓当组件的属性因为要支持故事或测试而变得宽泛且可选时。
  • ✓当你想移除实际应用从不使用的状态时。
  • ✓当你在重构 React 类型,并希望它们从实际代码推导出来时。

技能链接

https://github.com/humanlayer/humanlayer/blob/main/.claude/skills/narrow-react-prop-types/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/humanlayer/humanlayer/blob/main/.claude/skills/narrow-react-prop-types/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请把这个 React 卡片组件的类型收紧到实际应用所用的部分,它的可选属性太多了。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会先找到实际的非测试调用位置,然后让类型与它们一致。之后再更新测试和故事代码,使其适配更严格的类型。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Narrow React Prop Types

Use this skill when a React component's props have been widened for stories, mocks, tests, or demos and now express states the live application does not enter.

The goal is to make component types describe the real live-code-path contract, then require stories/tests/mocks to adapt to that contract instead of weakening it.

Core Requirements

  • Find the actual non-test, non-Storybook call sites before changing types.
  • Treat live code paths as the source of truth for the prop contract.
  • Do not preserve optional props only because they make Storybook, tests, or mock data easier.
  • Keep props optional only when there are non-Storybook, non-test call sites which do not provide them and which have a good reason for not doing so.
  • Types should not enable expressing states which are not observed in non-test, non-Storybook call sites.
  • Types should be as strict as possible so code can be as simple as possible.
  • Prefer deriving and extracting types from existing live-code-path values and APIs where possible.

Workflow

1. Identify the suspect component

Look for components with these signals:

  • Large props interfaces with many optional fields.
  • Optional callback calls such as onSelect?.(...) or onArchive?.(...).
  • Fallback state handling such as items ?? [], count ?? 0, or handler && ... around values live code likely always supplies.
  • UI affordances that always render even though their callbacks are optional.
  • Props that look demo-oriented, such as defaultFoo, alternate handler shapes, or display toggles not used by live code.

Do not pick a target from a story or test alone. Use stories/tests only as supporting evidence that the type has been widened, not as evidence that a state is real.

2. Find every live usage

Search for all imports/usages of the component, exported prop type, and shared child primitives.

Classify call sites by whether they are live code paths or support code:

  • Live code paths: app routes, wired components, providers, hooks, production package exports, and shared components used by those paths.
  • Support code: Storybook stories, test files, fixtures, mocks, demo harnesses, and visual-only examples.

Only live code paths should determine what the component API supports.

3. Derive the real types from the live code paths

Read the live call sites and classify each prop:

  • Required: every non-test, non-Storybook call site supplies it.
  • Optional: at least one non-test, non-Storybook call site omits it and that omission is a meaningful runtime state.
  • Removed: no non-test, non-Storybook call site uses it.

Nullability and optionality are different. If live code always passes a prop but the value can be empty, prefer a required nullable prop such as focusedItem: FocusedItem | null over focusedItem?: FocusedItem | null.

4. Tighten the public prop type

Update exported prop types to match only the states observed in live code paths.

The looser and more optional a type is, the more possible states the component has to reason about. Every optional prop creates another branch the component must handle, test, and keep correct. Prefer strict types that prevent impossible states instead of broad types that require defensive render logic.

If the component always renders an interactive affordance, require the handler that makes it work. Do not allow inert states like a visible menu item that calls onRename?.(...).

5. Derive and extract types where possible

Prefer deriving types from the live APIs instead of restating them manually:

  • Parameters<typeof fn>[0] for function argument types.
  • ReturnType<typeof fn> for return types.
  • Extract<Union, Shape> for narrowing a union to a real variant.
  • React.Dispatch<React.SetStateAction<T>> for React state setters instead of approximating them as (value: T) => void.

Prefer explicit state type parameters when inference would widen or obscure the intended state shape:

const [dialogState, setDialogState] = useState<DialogState>({
  id: null,
  isOpen: false,
})

Avoid relying on implicit useState(...) inference when it produces broad nullable object shapes, string literal widening, or callback types that later need hand-written approximations.

6. Tighten internal child props too

Do not stop at the exported component if it passes broad props into child primitives.

If row/menu/button child components receive optional handlers only because the parent props were

常见问题

它会破坏我的测试吗?

测试和故事可能需要更新,因为该技能会让它们适应更严格的类型,而不是削弱类型。

它会先检查实际应用吗?

会。在修改任何类型之前,它会先找到真正的非测试调用位置。

有些属性可以保持可选吗?

可以,但仅当某个真实的调用位置没有提供它,并且有充分理由时。

详情

作者的原始描述

narrow React component prop types to match live code paths

repo: humanlayer/humanlayer

热度

GitHub 星标
13k
排名
#11388
许可证
Apache-2.0
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能