Commit d7152f6a authored by Lukáš Huvar's avatar Lukáš Huvar Committed by Luis Fernando Alvarez D

Update typescript example to reflects out of box usage (#7235)

parent 693ba44a
{
"presets": [
"next/babel",
"@zeit/next-typescript/babel"
]
}
\ No newline at end of file
...@@ -6,7 +6,7 @@ type ListDetailProps = { ...@@ -6,7 +6,7 @@ type ListDetailProps = {
item: IDataObject; item: IDataObject;
} }
const ListDetail: React.FC<ListDetailProps> = ({ item: user }) => ( const ListDetail: React.FunctionComponent<ListDetailProps> = ({ item: user }) => (
<div> <div>
<h1>Detail for {user.name}</h1> <h1>Detail for {user.name}</h1>
<p>ID: {user.id}</p> <p>ID: {user.id}</p>
......
const withTypescript = require('@zeit/next-typescript')
module.exports = withTypescript()
...@@ -9,15 +9,14 @@ ...@@ -9,15 +9,14 @@
}, },
"dependencies": { "dependencies": {
"@zeit/next-typescript": "^1.1.1", "@zeit/next-typescript": "^1.1.1",
"next": "^7.0.2", "next": "^8.1.1-canary.23",
"react": "^16.7.0", "react": "^16.7.0",
"react-dom": "^16.7.0" "react-dom": "^16.7.0"
}, },
"devDependencies": { "devDependencies": {
"@types/next": "^7.0.6", "@types/node": "^11.13.9",
"@types/react": "^16.4.16", "@types/react": "^16.8.15",
"@types/react-dom": "16.0.11", "@types/react-dom": "^16.0.11"
"typescript": "3.2.4"
}, },
"license": "ISC" "license": "ISC"
} }
import * as React from 'react' import * as React from 'react'
import { NextContext } from 'next' import { NextPageContext } from 'next'
import Layout from '../components/Layout' import Layout from '../components/Layout'
import IDataObject from '../interfaces' import IDataObject from '../interfaces'
import { findData } from '../utils/sample-api' import { findData } from '../utils/sample-api'
import ListDetail from '../components/ListDetail'; import ListDetail from '../components/ListDetail';
type RequestQuery = {
id: string,
}
type Props = { type Props = {
item?: IDataObject, item?: IDataObject,
errors?: string, errors?: string,
} }
class ListDetailPage extends React.Component<Props> { class ListDetailPage extends React.Component<Props> {
static getInitialProps = async ({ query }: NextContext<RequestQuery>) => { static getInitialProps = async ({ query }: NextPageContext) => {
try { try {
const item = await findData(query.id); const { id } = query
const item = await findData(Array.isArray(id) ? id[0]: id);
return { item } return { item }
} catch (err) { } catch (err) {
return { errors: err.message } return { errors: err.message }
......
import * as React from 'react' import * as React from 'react'
import Link from 'next/link' import Link from 'next/link'
import Layout from '../components/Layout' import Layout from '../components/Layout'
import { NextPage } from 'next';
const IndexPage: React.FunctionComponent = () => { const IndexPage: NextPage = () => {
return ( return (
<Layout title="Home | Next.js + TypeScript Example"> <Layout title="Home | Next.js + TypeScript Example">
<h1>Hello Next.js 👋</h1> <h1>Hello Next.js 👋</h1>
......
import * as React from 'react' import * as React from 'react'
import { NextContext } from 'next' import { NextPageContext } from 'next'
import Link from 'next/link'; import Link from 'next/link';
import Layout from '../components/Layout' import Layout from '../components/Layout'
...@@ -13,7 +13,7 @@ type Props = { ...@@ -13,7 +13,7 @@ type Props = {
} }
class ListClass extends React.Component<Props> { class ListClass extends React.Component<Props> {
static async getInitialProps({ pathname }: NextContext) { static async getInitialProps({ pathname }: NextPageContext) {
// Example for including initial props in a Next.js page. // Example for including initial props in a Next.js page.
// Don't forget to include the respective types for any // Don't forget to include the respective types for any
// props passed into the component // props passed into the component
......
import { NextFunctionComponent, NextContext } from 'next' import { NextPage } from 'next'
import Link from 'next/link'; import Link from 'next/link';
import Layout from '../components/Layout' import Layout from '../components/Layout'
import List from '../components/List' import List from '../components/List'
import IDataObject from '../interfaces' import IDataObject from '../interfaces'
...@@ -11,7 +10,7 @@ type Props = { ...@@ -11,7 +10,7 @@ type Props = {
pathname: string, pathname: string,
} }
const ListFunction: NextFunctionComponent<Props> = ({ items, pathname }) => ( const ListFunction: NextPage<Props> = ({ items, pathname }) => (
<Layout title="List Example (as Functional Component) | Next.js + TypeScript Example"> <Layout title="List Example (as Functional Component) | Next.js + TypeScript Example">
<h1>List Example (as Function Component)</h1> <h1>List Example (as Function Component)</h1>
<p>You are currently on: {pathname}</p> <p>You are currently on: {pathname}</p>
...@@ -20,7 +19,7 @@ const ListFunction: NextFunctionComponent<Props> = ({ items, pathname }) => ( ...@@ -20,7 +19,7 @@ const ListFunction: NextFunctionComponent<Props> = ({ items, pathname }) => (
</Layout> </Layout>
) )
ListFunction.getInitialProps = async ({ pathname }: NextContext) => { ListFunction.getInitialProps = async ({ pathname }) => {
// Example for including initial props in a Next.js function compnent page. // Example for including initial props in a Next.js function compnent page.
// Don't forget to include the respective types for any props passed into // Don't forget to include the respective types for any props passed into
// the component. // the component.
......
{ {
"compilerOptions": { "compilerOptions": {
"target": "esnext", "allowJs": true,
"module": "esnext", "alwaysStrict": true,
"esModuleInterop": true,
"isolatedModules": true,
"jsx": "preserve", "jsx": "preserve",
"lib": ["dom", "es2017"], "lib": ["dom", "es2017"],
"module": "esnext",
"moduleResolution": "node", "moduleResolution": "node",
"allowJs": true,
"noEmit": true, "noEmit": true,
"strict": true, "noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"noUnusedLocals": true, "noUnusedLocals": true,
"noUnusedParameters": true, "noUnusedParameters": true,
"removeComments": false, "strict": true,
"preserveConstEnums": true, "target": "esnext"
"sourceMap": true
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment