-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b5f9856
Showing
16 changed files
with
818 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env*.local | ||
|
||
# vercel | ||
.vercel | ||
|
||
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# TypeScript Next.js example | ||
|
||
This is a really simple project that shows the usage of Next.js with TypeScript. | ||
|
||
## Deploy your own | ||
|
||
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-typescript) | ||
|
||
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-typescript&project-name=with-typescript&repository-name=with-typescript) | ||
|
||
## How to use it? | ||
|
||
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example: | ||
|
||
```bash | ||
npx create-next-app --example with-typescript with-typescript-app | ||
``` | ||
|
||
```bash | ||
yarn create next-app --example with-typescript with-typescript-app | ||
``` | ||
|
||
```bash | ||
pnpm create next-app --example with-typescript with-typescript-app | ||
``` | ||
|
||
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). | ||
|
||
## Notes | ||
|
||
This example shows how to integrate the TypeScript type system into Next.js. Since TypeScript is supported out of the box with Next.js, all we have to do is to install TypeScript. | ||
|
||
``` | ||
npm install --save-dev typescript | ||
``` | ||
|
||
To enable TypeScript's features, we install the type declarations for React and Node. | ||
|
||
``` | ||
npm install --save-dev @types/react @types/react-dom @types/node | ||
``` | ||
|
||
When we run `next dev` the next time, Next.js will start looking for any `.ts` or `.tsx` files in our project and builds it. It even automatically creates a `tsconfig.json` file for our project with the recommended settings. | ||
|
||
Next.js has built-in TypeScript declarations, so we'll get autocompletion for Next.js' modules straight away. | ||
|
||
A `type-check` script is also added to `package.json`, which runs TypeScript's `tsc` CLI in `noEmit` mode to run type-checking separately. You can then include this, for example, in your `test` scripts. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { ReactNode } from 'react' | ||
import Link from 'next/link' | ||
import Head from 'next/head' | ||
|
||
type Props = { | ||
children?: ReactNode | ||
title?: string | ||
} | ||
|
||
const Layout = ({ children, title = 'This is the default title' }: Props) => ( | ||
<div> | ||
<Head> | ||
<title>{title}</title> | ||
<meta charSet="utf-8" /> | ||
<meta name="viewport" content="initial-scale=1.0, width=device-width" /> | ||
</Head> | ||
<header> | ||
<nav> | ||
<Link href="/">Home</Link> | <Link href="/about">About</Link> |{' '} | ||
<Link href="/users">Users List</Link> |{' '} | ||
<a href="/api/users">Users API</a> | ||
</nav> | ||
</header> | ||
{children} | ||
<footer> | ||
<hr /> | ||
<span>I'm here to stay (Footer)</span> | ||
</footer> | ||
</div> | ||
) | ||
|
||
export default Layout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as React from 'react' | ||
import ListItem from './ListItem' | ||
import { User } from '../interfaces' | ||
|
||
type Props = { | ||
items: User[] | ||
} | ||
|
||
const List = ({ items }: Props) => ( | ||
<ul> | ||
{items.map((item) => ( | ||
<li key={item.id}> | ||
<ListItem data={item} /> | ||
</li> | ||
))} | ||
</ul> | ||
) | ||
|
||
export default List |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as React from 'react' | ||
|
||
import { User } from '../interfaces' | ||
|
||
type ListDetailProps = { | ||
item: User | ||
} | ||
|
||
const ListDetail = ({ item: user }: ListDetailProps) => ( | ||
<div> | ||
<h1>Detail for {user.name}</h1> | ||
<p>ID: {user.id}</p> | ||
</div> | ||
) | ||
|
||
export default ListDetail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react' | ||
import Link from 'next/link' | ||
|
||
import { User } from '../interfaces' | ||
|
||
type Props = { | ||
data: User | ||
} | ||
|
||
const ListItem = ({ data }: Props) => ( | ||
<Link href="/users/[id]" as={`/users/${data.id}`}> | ||
{data.id}:{data.name} | ||
</Link> | ||
) | ||
|
||
export default ListItem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// You can include shared interfaces/types in a separate file | ||
// and then use them in any component by importing them. For | ||
// example, to import the interface below do: | ||
// | ||
// import { User } from 'path/to/interfaces'; | ||
|
||
export type User = { | ||
id: number | ||
name: string | ||
} |
Oops, something went wrong.