Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/with-typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.js
22 changes: 22 additions & 0 deletions examples/with-typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# TypeScript Next.js example
This is a really simple project that show the usage of Next.js with TypeScript.

## How to use it?
```
npm install # to install dependencies
npm run dev # to compile TypeScript files and to run next.js
```

Output JS files are aside the related TypeScript ones.

## To fix
In tsconfig.json the options `jsx="react"` compiles JSX syntax into nested React.createElement calls.
This solution doesn't work well with some Next.js features like `next/head` or `next/link`.
The workaround is to create JS files that just return the mentioned module and require them from TSX files.
Like

```js
import Link from 'next/link'

export default Link
```
6 changes: 6 additions & 0 deletions examples/with-typescript/components/MyComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as React from 'react'

export default () =>
<div>
<p>This is my component</p>
</div>
13 changes: 13 additions & 0 deletions examples/with-typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"scripts": {
"dev": "concurrently \"tsc --watch\" next"
},
"dependencies": {
"next": "latest"
},
"devDependencies": {
"@types/react": "^15.0.1",
"concurrently": "^3.1.0",
"typescript": "^2.1.5"
}
}
8 changes: 8 additions & 0 deletions examples/with-typescript/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as React from 'react'
import MyComponent from '../components/MyComponent'

export default () =>
<div>
<h1>Hello world</h1>
<MyComponent />
</div>
8 changes: 8 additions & 0 deletions examples/with-typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2015",
"jsx": "react",
"allowJs": true
}
}