Here are the simplified steps to create a Next.js React app and add TypeScript to the mix. This tutorial combines my Create a Next.js app and Add TypeScript to Next.js posts.
Let's use npx and create-next-app
.
npx \
create-next-app nextjs-app \
--use-npm \
--example \
"https://github.com/vercel/next-learn/tree/master/basics/learn-starter"
# Enter your app's directory
cd nextjs-app
# Create an empty TypeScript configuration file
touch tsconfig.json
# Install TypeScript, React Types, and Node Types
npm install --save-dev typescript @types/react @types/node
You can customize your TypeScript in tsconfig.json
.
npm run dev
# ready - started server on 0.0.0.0:3000, url: http://localhost:3000
You should be able to access the site at http://localhost:3000.
npm run build
npm start
# ready - started server on 0.0.0.0:3000, url: http://localhost:3000
index.js
into index.tsx
Your application is now ready to be written in TypeScript. The example we've used only has one file named index.js
. Rename it to index.tsx
and re-write the JavaScript code (and any new code you add to the app) in TypeScript.
You can watch my step-by-step video on How to build a Next.js app with React & TypeScript.