본문 바로가기
Q&A해결2026년 4월 24일1분 읽기

shadcn/ui v3 + react-hook-form + zod 통합 시 타입 에러

s
shadcn초보
조회 460댓글 1

shadcn/ui v3로 업그레이드 후 form 컴포넌트가 컴파일 에러를 냅니다.

Type 'ZodObject<...>' is not assignable to type 'Resolver<...>'

이전엔 resolver: zodResolver(schema) 그대로 잘 동작했는데, v3에서 form 컴포넌트가 까다롭게 굴어요. 어떻게 풀어야 하나요?

댓글 1

김김영삼7개월 전

shadcn/ui v3의 Form 컴포넌트가 react-hook-form 8 + zod 4와 함께 출시되면서 타입 시스템이 한 단계 더 엄격해졌습니다. 표면 에러는 하나지만 원인이 두세 갈래입니다.

1) 의존성 버전 정합 확인

// package.json — 호환 버전
"react-hook-form": "^8.0.0",
"@hookform/resolvers": "^4.0.0",
"zod": "^4.0.0"

흔한 함정: @hookform/resolvers 3.x가 남아있는 상태. 이건 zod 3를 기대하므로 zod 4 schema를 받으면 타입 불일치를 일으킵니다.

npm install @hookform/resolvers@latest zod@latest react-hook-form@latest

2) zod 4의 schema infer 변경

zod 4에서 z.infer가 더 정확해진 대신, 일부 케이스에서 추론된 타입이 react-hook-form의 FieldValues 제약을 만족하지 못합니다.

// schema 정의
const schema = z.object({
name: z.string().min(2),
age: z.coerce.number().int(),
})

type FormValues = z.infer<typeof schema>

const form = useForm<FormValues>({
resolver: zodResolver(schema),
defaultValues: { name: '', age: 0 }, // 명시 필수
})

3) defaultValues 누락 — v3의 새 강제

shadcn v3 Form은 모든 필드에 defaultValues를 요구합니다. 부분만 주거나 비우면 internal type 추론이 partial로 떨어지면서 resolver 타입과 안 맞습니다.

전체 작동 코드

'use client'

import { z } from 'zod'
import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import {
Form, FormControl, FormField, FormItem, FormLabel, FormMessage
} from '@/components/ui/form'
import { Input } from '@/components/ui/input'
import { Button } from '@/components/ui/button'

const schema = z.object({
email: z.string().email(),
password: z.string().min(8),
})

type FormValues = z.infer<typeof schema>

export function LoginForm() {
const form = useForm<FormValues>({
resolver: zodResolver(schema),
defaultValues: { email: '', password: '' },
})

function onSubmit(values: FormValues) {
console.log(values)
}

return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>이메일</FormLabel>
<FormControl><Input {...field} /></FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">로그인</Button>
</form>
</Form>
)
}

v3에서 자주 깨지는 패턴

  • z.coerce 사용 시 input/output 타입이 다름 → z.input<typeof schema> vs z.output<...> 혼동 주의
  • z.discriminatedUnion — v3 Form이 타입 인식을 못하는 경우 있음 → type FormValues = z.infer<...> 명시 필수
  • conditional fields — watch로 다른 필드 값 보고 분기 시 타입이 좁혀지지 않음 → 별도 type guard 함수

빠른 진단

// 타입 추론 결과 직접 확인
type _Check = z.infer<typeof schema>
// ^^^^^^ 마우스 hover로 확인

대부분 사례에서 (1) 의존성 버전 + (2) defaultValues 명시 두 가지 점검만으로 해결됩니다. 그래도 안 되면 schema 한 줄씩 줄여가며 어떤 zod 함수가 추론을 깨는지 이등분 디버깅이 답입니다.

Ctrl+Enter로 등록