## Getting Started: Building a Solid Foundation
Embarking on a Next.js project with TypeScript requires a structured approach from the outset. This guide walks you through creating a highly optimized boilerplate tailored for modern user interfaces and experiences. By leveraging the latest tools and configurations, you'll ensure scalability, maintainability, and exceptional performance.
Begin by initializing your project using the official [Next.js](https://github.com/vercel/next.js) template optimized for TypeScript:
```bash
npx create-next-app@latest my-app --typescript --tailwind --eslint --app --src-dir --import-alias "@/*"
```
This command sets up a new application with TypeScript, Tailwind CSS, ESLint, the App Router, a `src` directory, and path aliases. Navigate into your project folder and install additional dependencies for a professional workflow:
```bash
npm install class-variance-authority clsx tailwind-merge lucide-react
npx shadcn-ui@latest init
```
These packages enable dynamic styling ([Class Variance Authority](https://github.com/Waffle-Labs/cva), [clsx](https://github.com/lukeed/clsx), [Tailwind Merge](https://github.com/dcastil/tailwind-merge)) and iconic components ([Lucide React](https://github.com/lucide-icons/lucide)). shadcn/ui provides customizable, accessible UI primitives built on [Radix UI](https://github.com/radix-ui) and Tailwind.
## Organized File Structure for Scalability
A clean architecture prevents chaos as your app grows. Adopt this proven directory layout:
```
src/
├── app/
│ ├── (auth)/ # Parallel routes for auth flows
│ ├── (marketing)/ # Marketing pages
│ ├── dashboard/ # Protected routes
│ ├── layout.tsx # Root layout
│ ├── page.tsx # Home page
│ ├── globals.css # Global styles
│ └── favicon.ico
├── components/ # Reusable UI components
│ ├── ui/ # shadcn/ui components
│ └── icons.tsx
├── lib/ # Utilities
│ ├── utils.ts # cn() helper
│ └── validation.ts # Zod schemas
├── hooks/ # Custom React hooks
├── types/ # TypeScript definitions
├── styles/ # Tailwind config
└── public/ # Static assets
```
This structure separates concerns: App Router for routing, `components` for UI, `lib` for helpers, and `types` for shared interfaces. Parallel routes like `(auth)` and `(marketing)` allow flexible layouts without URL nesting.
## TypeScript Configuration for Type Safety
Enhance developer experience with a robust `tsconfig.json`:
```json
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [{ "name": "next" }],
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
```
Key features include strict mode for error prevention, path aliases (`@/*`), and Next.js plugin integration. This setup catches bugs early and supports IDE autocompletion seamlessly.
## Linting and Formatting: Code Quality Essentials
Maintain consistency with ESLint ([ESLint](https://github.com/eslint/eslint)) and Prettier ([Prettier](https://github.com/prettier/prettier)). Install Husky ([Husky](https://github.com/typicode/husky)) for Git hooks:
```bash
npm install -D husky lint-staged
npx husky init
```
Add to `package.json`:
```json
"lint-staged": {
"**/*.{ts,tsx,js,jsx}": ["eslint --fix", "prettier --write"]
}
```
Commit hooks will auto-format and lint, ensuring clean code. Extend ESLint with Next.js and React rules for comprehensive coverage.
## Styling Mastery with Tailwind CSS
[Tailwind CSS](https://github.com/tailwindlabs/tailwindcss) delivers utility-first styling. Customize `tailwind.config.js`:
```js
import type { Config } from 'tailwindcss'
export default {
content: ['./src/**/*.{js,ts,jsx,tsx,mdx}'],
theme: { extend: {} },
plugins: [],
} satisfies Config
```
Create a `lib/utils.ts` helper:
```ts
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
```
Use `cn()` in components for conditional classes, e.g., `<div className={cn('p-4', isActive && 'bg-blue-500')}>Content</div>`.
## Building UI with shadcn/ui
Install shadcn/ui components on-demand:
```bash
npx shadcn-ui@latest add button card dialog
```
These copy-paste components are fully customizable. Example button:
```tsx
import { Button } from '@/components/ui/button'
export function MyButton() {
return <Button variant="outline">Click me</Button>
}
```
Advanced variants use `cva` for themeable styles, ensuring design system consistency.
## Advanced Forms with React Hook Form and Zod
Handle forms robustly with [React Hook Form](https://github.com/react-hook-form/react-hook-form) and [Zod](https://github.com/colinhacks/zod):
```bash
npm install react-hook-form @hookform/resolvers zod
npx shadcn-ui@latest add form input label
```
Define schemas in `lib/validation.ts`:
```ts
import { z } from 'zod'
export const formSchema = z.object({
email: z.string().email(),
})
```
Integrate in components:
```tsx
import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
const form = useForm({
resolver: zodResolver(formSchema),
})
```
This provides server/client validation, type inference, and minimal re-renders.
## Data Fetching and State Management
Use [TanStack Query](https://github.com/TanStack/query) for caching and mutations:
```bash
npm install @tanstack/react-query
```
Wrap your app:
```tsx
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
const queryClient = new QueryClient()
<QueryClientProvider client={queryClient}>
{/* App */}
</QueryClientProvider>
```
Fetch data:
```tsx
const { data } = useQuery({
queryKey: ['todos'],
queryFn: () => fetch('/api/todos').then(res => res.json()),
})
```
Perfect for dynamic UIs with optimistic updates.
## Smooth Animations with Framer Motion
Add delight with [Framer Motion](https://github.com/framer/motion):
```bash
npm install framer-motion
```
Animate components:
```tsx
import { motion } from 'framer-motion'
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.3 }}
>
Content
</motion.div>
```
From micro-interactions to page transitions, it elevates UX.
## Performance Optimizations
- **Image Optimization**: Use `next/image` with `sizes` and `priority`.
- **Font Optimization**: Preload Google Fonts in `layout.tsx`.
- **Bundle Analysis**: Run `next build` and analyze with `@next/bundle-analyzer`.
- **Lazy Loading**: Dynamic imports: `const DynamicComponent = dynamic(() => import('./Component'))`.
## SEO and Metadata
Leverage App Router metadata:
```tsx
export const metadata = {
title: 'My App',
description: 'Description',
}
```
Open Graph and structured data for social sharing.
## Deployment Ready
Deploy to Vercel effortlessly: Connect GitHub, auto-deploys on push. Environment variables and previews out-of-the-box.
For a complete starter, check the [optimized Next.js TypeScript repo](https://github.com/cursor-directory/optimized-nextjs-typescript-best-practices-modern-ui-ux). This setup powers production apps with lightning speed and pixel-perfect design.
Apply these practices progressively: Start simple, layer on complexity. Your users will notice the polish.
<div style="text-align: center; margin-top: 2rem;">
<a href="https://cursor.directory/optimized-nextjs-typescript-best-practices-modern-ui-ux" target="_blank" rel="noopener noreferrer" class="view-full-resource-btn" style="display: inline-block; background-color: #f97316; color: white; padding: 12px 24px; border-radius: 8px; text-decoration: none; font-weight: 600; transition: background-color 0.2s;">View Full Resource</a>
</div>