Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Using Next.js with TypeScript

Anjali Ariscrisnã
Admilson Cruz

March 13, 2023

Min Read
Using Next.js with TypeScript
blue arrow to the left
Imaginary Cloud logo
blue arrow to the left
Imaginary Cloud logo

What is Next.js?

Next.js is an open-source framework created by Vercel. It claims to be the Web's Software Development Kit with all the tools needed "to make the Web. Faster" (sic). Learn about Next.js features with React and its applications here.

What is Next.js used for?

Next.js enables search engines to easily optimize React apps with zero configuration. A traditional React app is rendered on the client-side where the browser starts with a shell of an HTML page lacking any rendered content. From there, the browser fetches the JavaScript file containing the React code to render content to the page and make it interactive. However, there are two major drawbacks with client-side rendering:

1. The content is not reliably indexed by all search engines or read by social media link bots;

2. It can take longer to reach the first contentful paint when a user first lands on the web page.

Next.js is a framework that allows you to build a React app but render the content in advance on the server so the first thing a user or search bot sees is the fully rendered HTML. After receiving this initial page, client-side rendering takes over and it works just like a traditional React app. It’s the best of both worlds: fully rendered content for bots, and highly interactive content for users.

Client-side data fetching using Next.js

The real magic comes into play when we talk about data fetching because Next.js can perform multiple server rendering strategies from a single project. Client-side data fetching is useful when your page doesn't require SEO indexing, when you don't need to pre-render your data, or when the content of your pages needs to update frequently. Static generation or pre-rendering allows you to render your pages at build time. See the example below.


It is possible to see in the previous code snippet an example of Client-side data fetching using the useEffect hook of React.

First, we initialised constants to check if the fetching is still pending and to save the data resulting from the fetching process. Then, we call the useEffect hook with 2 different arguments:

  • Callback - function containing the side-effect login that runs right after the changes were being pushed to DOM. In our case, the logic is to fetch data from an endpoint and save it into our constant.
  • Dependencies - an array of dependencies that allows specifying when the callback should run. By passing an empty array, it means that we want the callback to run only once.
blue arrow to the left
Imaginary Cloud logo

What is TypeScript?

TypeScript is a programming language that is developed and maintained by Microsoft, and it is a strict superset of JavaScript. It supports static and dynamic typing, and further provides inheritance features, classes, visibility scopes, namespaces, interfaces, unions, and other modern features. TS was designed to handle larger projects as it’s easier to refactor code. Learn more about its features with an in-depth comparison with JavaScript.

blue arrow to the left
Imaginary Cloud logo

What do you use TypeScript for?

There are reasons galore why a JavaScrip developer considers using TypeScript:

  • Using new features of ECMAScript - TypeScript supports ECMAScript standards and transpile them to ECMAScript targets of your choice, so you can use features like modules, lambda functions, classes, restructuring, amongst others.
  • Static typing - JavaScript is a dynamical type and it does not know what type of variable is until it is actually instantiated at runtime; here, TypeScript adds type support to the JavaScript.
  • Type Inference - TypeScript makes typing a little bit easier and a lot less explicit by the usage of type inference. Even if you don’t explicitly type the types, they are still there to save you from doing something which otherwise would result in a runtime error.
  • Better IDE Support - The development experience with TypeScript is a great improvement over JavaScript. There is a wide range of IDEs that have excellent support for TypeScript, like the Visual Studio and VS Code, IntelliJ and Sublime, or WebStorm.
  • Strict Null Checking - Errors like “you cannot read a property ‘x’ of undefined” are common in JavaScript programming. You can avoid most of these kinds of errors through strict checking since one cannot use a variable that is not known to the TypeScript compiler.
  • Interoperability - TypeScript is closely related to JavaScript so it has great interoperability capabilities, but some extra work is required to work with the JavaScript libraries in TypeScript.

18 best Agile practices to use in your Software Development Cycle
blue arrow to the left
Imaginary Cloud logo

How do I install TypeScript in NextJS?

Here’s a step-by-step guide to install TypeScript in a Next.js app:

1. Create base project - command: npx create-next-app next-app-example.
Create project with a base template.

2. Add tsconfig.json into the root of the project to active TypeScript.

3. Structure the project in the form.


4. Create TypeScript types in Next.js

You can create types for anything in your application, including props types, API responses, arguments for functions, and so on.

We first create a type for our todos:


5. Create components in Next.js

Now that we have our ITODO type, we can create the Todo.tsx component.


As you can see, we start by importing the previous type we created, and also creating another one called Props, which will mirror the props received as parameters by the component.

This component is responsible for displaying the ITodo object. This component receives the ITodo object, a markTodo function, and a removeTodo function as props. Notice that this argument has to match the props type in order to make Typescript happy.

Now let's create our FormTodo.tsx component, responsible for adding Todos in our app.


Our component accepts the addTodo as a parameter. It handles the submission of a new Todo. If the value is not empty, then we call the addTodo function on that todo text and then set the value of the form to empty again. This component returns a form that accepts todos and has a submit button, which by clicking the button, we add the todo in the todo list.

6. Create our page in order to use our react components.
We will start by importing the components and types we created earlier.

After importing the components and types, we imported InferGetStaticPropsType, which is provided by Next.js allowing us to set the type on the method getStaticProps.

After that we initialized our todoList using the useState hook, passing as an argument our initial todos provided by the getStaticProps.

Finally, we declared our main fuctions that implement our logic:

  • addTodo - allows to add a todo in our list
  • removeTodo - allows to remove a todo in our list
  • markTodo - allows to set a todo as done in our list

In the end, we return a list of our todos, using our components.

New call-to-action

Found this article useful? You might like these ones too!

blue arrow to the left
Imaginary Cloud logo
blue arrow to the left
Imaginary Cloud logo
blue arrow to the left
Imaginary Cloud logo
blue arrow to the left
Imaginary Cloud logo
blue arrow to the left
Imaginary Cloud logo
blue arrow to the left
Imaginary Cloud logo
Anjali Ariscrisnã
Anjali Ariscrisnã

Versatile and data-driven Growth Marketer with in-depth business knowledge, updated with latest developments in the Digital Marketing landscape.

Read more posts by this author
Admilson Cruz
Admilson Cruz

A young and passionate developer who is on a quest to make a difference in how people go about their daily lives.

Read more posts by this author

People who read this post, also found these interesting:

arrow left
arrow to the right
Dropdown caret icon