How to Set Up a NextJS Project with TypeScript

How to Set Up a NextJS Project with TypeScript

Begin

I want to record how to setting up a NextJS project with TypeScript.

Let’s go

Create a NextJS Project

1
yarn create next-app --typescript [your project path]

Yes, I used yarn. I prefer yarn to npm.

This article is very subjective. If you do not feel comfortable reading it. Please close it as soon as possible.
If you think my article can help you, you can subscribe to this site by using RSS.

Add an EditorConfig File

Add .editorconfig file to root directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,tsx}]
charset = utf-8
indent_style = space
indent_size = 2


# Matches the exact files either package.json or .travis.yml
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2

Install Google TypeScript code style tools

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
npx install gst

.
.
.
Need to install the following packages:
gts
Ok to proceed? (y) y

.
.
.
Already have devDependency for typescript:
-4.3.2
+^4.0.3
? Overwrite No
# I want to used latest TypeScript version. So I choose No

.
.
.
./tsconfig.json already exists
? Overwrite No
# We don't need to overwrite this file.

and need add a line code to tsconfig.json

1
2
3
4
5
{
...
"extends": "./node_modules/gts/tsconfig-google.json",
...
}

and remove src/index.ts

1
rm src/index.ts

Add VSCode setting.json

I used VSCode
Create new file .vscode/settings.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"editor.renderWhitespace": "all",
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.formatOnSave": true
},
"[javascriptrect]": {
"editor.formatOnSave": true
},
"[json]": {
"editor.formatOnSave": true
},
"[typescript]": {
"editor.formatOnSave": true
},
"[typescriptreact]": {
"editor.formatOnSave": true
}
}

Add TailWindCSS

I love tailwindcss. It is very useful. So I will add it on project.

Install Tailwind via

1
yarn add -D tailwindcss@latest postcss@latest autoprefixer@latest

Create your configuration files

1
npx tailwindcss init -p

Configure Tailwind to remove unused styles in production

1
2
3
4
5
6
7
8
9
10
11
12
// tailwind.config.js
module.exports = {
purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}

Include Tailwind in your CSS

1
2
3
4
/* ./styles/globals.css */
@tailwind base;
@tailwind components;
@tailwind utilities;

Now you can begin your work!

1
yarn dev

Reference

NextJS Document
What is EditorConfig?
Google TypeScript Style
Getting started with Tailwind CSS

Photo by Ferenc Almasi on Unsplash

Author

Ewan Xiao

Posted on

June 8th 2021

Updated on

May 17th 2024

Licensed under

Comments