Search AI Tools

Agent skill

typescript-unknown-jsx-expression

Fix TypeScript error "Type 'unknown' is not assignable to type ReactNode" when using Record<string, unknown> values directly in JSX && short-circuit expressions. Use when: (1) JSX expression {someObj.field && <Component />} fails with TS2322, (2) Reading from a JSON or untyped object via bracket notation in JSX conditionals, (3) Values typed as unknown appear in JSX even when wrapped in String() or guarded with truthiness checks. Fix: use !! to coerce to boolean before the && operator.

Stars 0
Forks 0

Install this agent skill to your Project

npx add-skill https://github.com/yorch/claude-skills/tree/main/typescript-unknown-jsx-expression

SKILL.md

TypeScript unknown in JSX && Expressions

Problem

When using Record<string, unknown> (or any unknown-typed values) in JSX conditional rendering with &&, TypeScript raises:

Type 'unknown' is not assignable to type 'string | number | bigint | boolean |
ReactElement<...> | Iterable<ReactNode> | ReactPortal | Promise<...> | null | undefined'

Even when the value is wrapped in String(), the error still occurs because the JSX expression itself evaluates to unknown | JSX.Element.

Context / Trigger Conditions

  • Object is typed as Record<string, unknown> (common with JSON fields from APIs)
  • JSX contains: {obj.field && <Component value={String(obj.field)} />}
  • Error TS2322 on the && line, not inside the Component
  • TypeScript strict mode enabled

Root Cause

In TypeScript, a && b returns the type of a when a is falsy — so unknown && JSX.Element resolves to unknown | JSX.Element. TypeScript cannot prove unknown is a valid ReactNode, so it rejects the expression.

Solution

Force a boolean result with !! before the &&:

tsx
// ❌ FAILS — wd.entry_time is unknown
{wd.entry_time && (
  <DetailField value={String(wd.entry_time)} />
)}

// ✅ WORKS — !!wd.entry_time is boolean
{!!wd.entry_time && (
  <DetailField value={String(wd.entry_time)} />
)}

// Also works with null check
{wd.entry_time != null && (
  <DetailField value={String(wd.entry_time)} />
)}

For chained &&:

tsx
// ❌ FAILS — first && returns unknown
{wd.needs_reentry && wd.reentry_date && (
  <DetailField value={String(wd.reentry_date)} />
)}

// ✅ WORKS — both forced to boolean
{!!wd.needs_reentry && !!wd.reentry_date && (
  <DetailField value={String(wd.reentry_date)} />
)}

Verification

bash
yarn frontend:typecheck  # or tsc --noEmit
# Should produce no TS2322 errors on the affected lines

Notes

  • This is not a React issue — it's a TypeScript type-narrowing limitation with unknown
  • String(unknown) returns string (which is valid ReactNode), but the issue is the JSX expression type before String() is applied
  • Alternative: cast the whole object with a more specific type at the call site const wd = (r.workDetails ?? {}) as Record<string, string | boolean | undefined> — this eliminates all !! guards but reduces type safety
  • !! is the idiomatic JS/TS way to coerce any value to boolean

References

Expand your agent's capabilities with these related and highly-rated skills.

yorch/claude-skills

prisma-7-docker-migrations

Run Prisma 7 migrations inside a multi-stage Docker production image. Use when: (1) `prisma migrate deploy` crashes with "Cannot resolve environment variable: DATABASE_URL" or "cannot find module" at container startup, (2) building a production Docker image that needs to run migrations before starting the server, (3) you copied only node_modules/prisma + node_modules/@prisma but the Prisma 7 CLI still fails with MODULE_NOT_FOUND at runtime, (4) Yarn 4 node-modules linker .bin/prisma symlink breaks after Docker COPY. Covers prisma.config.ts (Prisma 7), full node_modules copy requirement, and symlink recreation pattern.

0 0
Explore
miantiao-me/aigc-weekly

batch-research

批量数据采集技能,负责分批并发调度 researcher agent 抓取所有数据源。

517 60
Explore
miantiao-me/aigc-weekly

chinese-writing

专注科技周刊的中文写作与审稿技能指南,指导大模型生成与审核清晰、克制、无 AI 味的高质量周刊内容。

517 60
Explore
miantiao-me/aigc-weekly

publish-weekly

使用 Payload REST API 将周刊发布到 CMS,正文不包含头部元数据,通过 users API Key 认证。

517 60
Explore
mattpocock/skills

edit-article

Edit and improve articles by restructuring sections, improving clarity, and tightening prose. Use when user wants to edit, revise, or improve an article draft.

111,310 9,758
Explore
mattpocock/skills

setup-pre-commit

Set up Husky pre-commit hooks with lint-staged (Prettier), type checking, and tests in the current repo. Use when user wants to add pre-commit hooks, set up Husky, configure lint-staged, or add commit-time formatting/typechecking/testing.

111,310 9,758
Explore

Didn't find tool you were looking for?

Be as detailed as possible for better results