Home/Answers/Optional fields
JSON→TS / Optional fields

Optional fields Examples

For example, use Optional fields for sharing/archiving: back up, export to the target format and spot-check 2-3 files.

Updated 2026-10-31About 3 minLocal test
Quick answer

For example, use Optional fields for sharing/archiving: back up, export to the target format and spot-check 2-3 files. Use `?` for possibly missing keys; `| null` for null values.

Why it matters

Real scenarios expose problems better than abstract specs.

`?` includes undefined.

Step by step

  1. Start with real/complete JSON covering optional fields and empty arrays.
  2. Check array elements, null/optional and nested objects.
  3. Set the root name and copy into the project.
  4. Add Zod/Valibot runtime validation for production.
  5. Add contract tests/versioning for API changes.

Common mistakes

  • Treating a sample as a complete schema, missing optional/empty arrays.
  • Trusting generated any/null unions as-is.
  • Forgetting quotes for property names with spaces/Chinese.
  • Skipping runtime validation after generation.

Checklist

  • Sample covers optional/empty arrays/null.
  • Array object fields merged.
  • Property/root names valid.
  • Runtime validation added.
  • API contract/version considered.

Key facts

  • `?` includes undefined.
  • exactOptionalPropertyTypes can be stricter.

FAQ

Can you show a Optional fields example?

For example, use Optional fields for sharing/archiving: back up, export to the target format and spot-check 2-3 files. Use `?` for possibly missing keys; `| null` for null values.

How long does it take?

Usually 1-3 minutes; for intermittent issues, run 30 seconds and retest elsewhere.

Is data uploaded?

Runs locally and does not upload video/audio; extensions may still read the page.

Source: TypeScript optional