Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Typescript example and config files #1610

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions examples/with-typescript/README.md
Expand Up @@ -9,11 +9,6 @@ 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'
Expand Down
10 changes: 6 additions & 4 deletions examples/with-typescript/package.json
Expand Up @@ -3,11 +3,13 @@
"dev": "concurrently \"tsc --watch\" next"
},
"dependencies": {
"next": "latest"
"next": "2.0.1"
},
"devDependencies": {
"@types/react": "^15.0.1",
"@types/react": "^15.0.21",
"concurrently": "^3.1.0",
"typescript": "^2.1.5"
"typescript": "2.2.1",
"react": "15.4.2",
"react-dom": "15.4.2"
}
}
}
9 changes: 5 additions & 4 deletions examples/with-typescript/pages/index.tsx
@@ -1,8 +1,9 @@
import * as React from 'react'
import Link from 'next/link'
import MyComponent from '../components/MyComponent'

export default () =>
export default () =>
<div>
<h1>Hello world</h1>
<p>Hello there</p>
<MyComponent />
</div>
<div>Click <Link prefetch href={{ pathname: 'pagewithasync' }}><a>Reactpage</a></Link> </div>
</div>
28 changes: 28 additions & 0 deletions examples/with-typescript/pages/pagewithasync.tsx
@@ -0,0 +1,28 @@
import * as React from 'react'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe consider camelCasing in the filename?

import 'isomorphic-fetch'

interface IPageProps {
stars: number
}

interface IPageState {
clicked: boolean
}

export default class extends React.Component<IPageProps, IPageState> {
static async getInitialProps() {
const res = await fetch('https://api.github.com/repos/zeit/next.js');
const data = await res.json();
return { stars: data.stargazers_count }
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe some space after these } as well @giladgray?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import` React from 'react'
import 'isomorphic-fetch'
export default class extends React.Component {
static async getInitialProps () {
const res = await fetch('https://api.company.com/user/123')
const data = await res.json()
return { username: data.profile.username }
}
}

From https://zeit.co/blog/next

Copy link

@giladgray giladgray Apr 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes please blank line after every function, class, member, etc.

a little tslint goes a long way.

public state: { clicked: false }
private click = () => {
this.setState({ clicked: true })
}
render() {
return <div onClick={this.click}>
<h1>Next has {this.props.stars} stars</h1>
<span>{this.state.clicked ? 'clicked' : 'please click'}</span>
</div>
}
}
14 changes: 9 additions & 5 deletions examples/with-typescript/tsconfig.json
@@ -1,8 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2015",
"jsx": "react",
"allowJs": true
"module": "es2015",
"target": "es2017",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you changing these two fields? this is a pretty significant shift in how the example works. what's wrong with commonjs es6?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the pull request comment

"jsx": "react-native",
"moduleResolution": "node",
"lib": [
"dom",
"es2017"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed? async/await are supported natively in ts 2.2

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See update PR comment above

]
}
}
}