Skip to content

Commit

Permalink
Fix tsx workaround, bootstrap next.d.ts, add tslint deps (#2885)
Browse files Browse the repository at this point in the history
  • Loading branch information
brikou authored and timneutkens committed Nov 13, 2017
1 parent 7ee66de commit 45e26f2
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 38 deletions.
2 changes: 1 addition & 1 deletion examples/with-typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
**/*.js
*.js
19 changes: 5 additions & 14 deletions examples/with-typescript/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/with-typescript)

# 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
npm install
npm run dev
```

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: 0 additions & 6 deletions examples/with-typescript/components/MyComponent.tsx

This file was deleted.

10 changes: 10 additions & 0 deletions examples/with-typescript/next.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare module 'next/link' {
import { Url } from 'url'

export default class Link extends React.Component<
{
href: string | Url;
},
{}
> {}
}
17 changes: 11 additions & 6 deletions examples/with-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
"name": "with-typescript",
"version": "1.0.0",
"scripts": {
"dev": "concurrently \"tsc --watch\" next"
"dev": "concurrently \"tsc --pretty --watch\" \"next\"",
"prebuild": "tsc",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^16.0.0",
"react-dom": "^16.0.0"
"react": "16.1.0",
"react-dom": "16.1.0"
},
"devDependencies": {
"@types/react": "^16.0.9",
"concurrently": "^3.1.0",
"typescript": "^2.1.5"
"@types/node": "8.0.51",
"@types/react": "16.0.22",
"concurrently": "^3.5.0",
"tslint": "5.8.0",
"typescript": "2.6.1"
}
}
1 change: 1 addition & 0 deletions examples/with-typescript/pages/about.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <div>About us</div>
11 changes: 6 additions & 5 deletions examples/with-typescript/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as React from 'react'
import MyComponent from '../components/MyComponent'
import Link from 'next/link'

export default () =>
export default () =>
<div>
<h1>Hello world</h1>
<MyComponent />
Hello World.{' '}
<Link href="/about">
<a>About</a>
</Link>
</div>
12 changes: 6 additions & 6 deletions examples/with-typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2015",
"jsx": "react",
"allowJs": true
}
"compilerOptions": {
"jsx": "react-native",
"module": "commonjs",
"strict": true,
"target": "es2017"
}
}
10 changes: 10 additions & 0 deletions examples/with-typescript/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"defaultSeverity": "error",
"extends": ["tslint:recommended"],
"jsRules": {},
"rules": {
"quotemark": [true, "single", "jsx-double"],
"semicolon": [true, "never"]
},
"rulesDirectory": []
}

0 comments on commit 45e26f2

Please sign in to comment.