go back

Adding TypeScript support to your NextJS application

Sourav Raveendran

4 min read

2021-09-19

A detailed walkthrough for using TypeScript with your NextJS application.

NextjsTypescript

Share this article

FacebookX
Adding TypeScript support to your NextJS application

Why Typescript

TypeScript is a strongly typed programming language which builds on JavaScript giving you better tooling at any scale. As TypeScript is a superset of JavaScript, existing JavaScript programs are also valid TypeScript programs.

Ease of Use

One of the big benefits of TypeScript is that it enables IDE's to provide a richer environment for spotting common errors as you type the code.

For a large JavaScript project, adopting TypeScript might result in more robust software, while still being deployable where a regular JavaScript application would run.

It is open source, and you get the clever intellisense as you type if you use a supported IDE like Microsoft's Visual Studio Code.

NextJS provides an integrated TypeScript experience out of the box which makes our life's much easier.

Installation

Let's begin by opening your Terminal and running the following command

Terminal
npx create-next-app next-typescript-example && cd next-typescript-example

this will create a new NextJS project directory named next-typescript-example and change to that directory.

Create a new tsconfig.json file in the root of your directory.

Terminal
touch tsconfig.json

Now, try restarting the developement server (npm run dev or yarn dev). It should spit out some warnings like so.

Terminal
# It looks like you're trying to use TypeScript but do not have the required
# package(s) installed.

# Please install typescript, @types/react, and @types/node by running:

# yarn add --dev typescript @types/react @types/node

this is because we have not installed TypeScript or its accompnying types for our project, so lets go ahead and install them.

Terminal
# If you're using npm
npm install --save-dev typescript @types/react @types/node

# If you're using yarn
yarn add --dev typescript @types/react @types/node

Now restart the development server again, NextJS will automaticall populate the tsconfig.json file and have created the next-env.d.ts files, try not to change anything within this file, now you've added Typescript to your NextJS app.

Now let's rename the files to ts and tsx files, a .tsx files is a TypeScript file written using JSX syntax. Try to be consistent with naming of the files in your project although a .ts file may export a TSX Component it is not considered as a good convention to do so and might create confusions with your collegues when you are working in a major Project.

Editor
pages/api/*.js      --> pages/api/*.ts
pages/_app.js       --> pages/_app.tsx
pages/index.js      --> pages/index.tsx
pages/_document.js  --> pages/_document.tsx

with that done go to your pages/_app.ts file and use the built-in type AppProps, like so:

_app.ts
import { AppProps } from 'next/app'

function App({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />
}

export default App

with that we've converted our JavaScript NextJS app to a TypeScript one. here are some most commonly used NextJS specific types that you will run into.

Data Fetching Methods

getStaticProps, getStaticPaths, getServerSideProps types:

index.tsx
import { GetStaticProps, GetStaticPaths, GetServerSideProps } from 'next'

export const getStaticProps: GetStaticProps = async context => {
  // ...
}

export const getStaticPaths: GetStaticPaths = async () => {
  // ...
}

export const getServerSideProps: GetServerSideProps = async context => {
  // ...
}

API Routes

Request, Response types:

import { NextApiRequest, NextApiResponse } from 'next'

export default (req: NextApiRequest, res: NextApiResponse) => {
  // ...
}

That's it! Your app is now powered by Typescript. you can learn more about using NextJS in TypesScript by referring to NextJS official TypeScript documentation.

popular post

Adding Embla Carousel to your NextJS application

8516 views

A detailed walkthrough for setting up and using Embla Carousel in your NextJS application.

Read Article

Using ChartJS a charting library in your NextJS application

4718 views

A detailed walkthrough for setting up and using ChartJS a simple yet flexible charting library in your NextJS application.

Read Article

Auto advancing Carousel component in Next JS.

2178 views

A detailed walkthrough for implementing auto advancing feature for your Embla Carousel.

Read Article

Using Preact with your NextJS application

546 views

A detailed walkthrough for setting up and using Preact a lightweight altrenative to React in your NextJS application.

Read Article