---
title: "Discussion on \"使用zod时的注意：nullable vs optional\" | Hashnode"
description: "Discussion on \"使用zod时的注意：nullable vs optional\". 定义zod schema的时候，有时候定义为nullable时要注意：如果你是从一个可能为空的对象里取出一个字段来parse的时候，可能会出现问题：\nconst schema = z.object({\n  a: z.string().nullable()\n});\n\nconst rawData = {\n  nested: null\n};\n\nconst data = schema.parse({\n  a: rawData.nested?.a\n});\n\n会导致throw，程序无法继续下去。\n解决办法：..."
url: "https://hashnode.com/posts/zodnullable-vs-optional/6743dbd0bd6310378e46822b"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [使用zod时的注意：nullable vs optional](https://landay.hashnode.dev/zodnullable-vs-optional?utm_source=hashnode&utm_medium=feed)

定义zod schema的时候，有时候定义为nullable时要注意：如果你是从一个可能为空的对象里取出一个字段来parse的时候，可能会出现问题： const schema = z.object({ a: z.string().nullable() }); const rawData = { nested: null }; const data = schema.parse({ a: rawData.nested?.a }); 会导致throw，程序无法继续下去。 解决办法：...

Nov 25, 2024 · 1 min read · landay.hashnode.dev  
Read the article

-   [#zod](https://hashnode.com/tag/zod)
-   [#undefined](https://hashnode.com/tag/undefined)
-   [#null](https://hashnode.com/tag/null)
-   [#typescript](https://hashnode.com/tag/typescript)

## Written by

[Landay](https://hashnode.com/@Landay)

@Landay

  
[View profile](https://hashnode.com/@Landay)

## Responses

Markdown supported · ⌘↵ to post

No responses yet.
