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 のスキーマ定義が原因で"Type 'unknown' is not assignable to type ~"が発生した

0
Posted at

やりたいこと

API 通信を行うテストを成功させる

実行結果

テスト実行時に型エラーが発生しました。

エラー内容

Type 'unknown' is not assignable to type 'ItemResponseDto'.

原因調査

Zod のスキーマ定義を確認しました。

原因

z.object() を使用せず、通常のオブジェクトとして定義していました。

export const ItemSchema = {
  id: z.uuid(),
  name: z.string(),
  value: z.int().nullable(),
};

解決策

z.object() を使用して、Zod のオブジェクトスキーマとして定義しました。

export const ItemSchema = z.object({
  id: z.uuid(),
  name: z.string(),
  value: z.int().nullable(),
});

学んだこと

Zod でオブジェクトスキーマを定義する場合は、z.object() を使用します。

参考

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?