0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Zod】nullableとnullishの違い

Posted at

Zod の .nullable().nullish()undefind を許容するか。
.nullable()undefind を許容せず、 .nullish() は許容する。

.nullable()

.nullable()null のみを許容する。
使用方法は以下のとおり。

z.nullable() を使用する。

const nullableString = z.nullable(z.string());
nullableString.parse("asdf"); // => "asdf"
nullableString.parse(null); // => null

または、 .nullable() メソッドを使用する。

const E = z.string().nullable(); // equivalent to nullableString
type E = z.infer<typeof E>; // string | null

.nullish()

.nullish()nullundefind を許容する。
使用方法は以下のとおり .nullish() を使用する。

const nullishString = z.string().nullish(); // string | null | undefined

// equivalent to
z.string().nullable().optional();

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?