next js api typescript code example

Example 1: nextjs with typescrip example

npx create-next-app --example with-typescript with-typescript-app
# or
yarn create next-app --example with-typescript with-typescript-app

Example 2: next api typescript

import type { NextApiRequest, NextApiResponse } from 'next'

type Data = {
  name: string
}

export default (req: NextApiRequest, res: NextApiResponse<Data>) => {
  res.status(200).json({ name: 'John Doe' })
}

Example 3: next js typescript

// create a tsconfig
touch tsconfig.json

// run command `npm run dev` 
npm run dev