Skip to content

Instantly share code, notes, and snippets.

@koss-lebedev
Last active May 16, 2019 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koss-lebedev/96ec62dda5a98f36edc7dc30769a025a to your computer and use it in GitHub Desktop.
Save koss-lebedev/96ec62dda5a98f36edc7dc30769a025a to your computer and use it in GitHub Desktop.
import { NextContext } from 'next'
// we define our type
type Context = NextContext<{ subreddit: string }>
// ...
const getInitialProps = async (context: Context) => {
// now we can get subreddit value from the query!
const subreddit = context.query.subreddit
const response = await fetch(`https://www.reddit.com/r/${subreddit}.json`)
const result = await response.json() as RedditResult
return {
subreddit,
posts: result.data.children
}
}
/*
* But we get an error here :(
*
* Type '(context: NextContext<{ subreddit: string; }, {}>) =>
* Promise<{ subreddit: string; posts: readonly RedditPost[]; }>' is not assignable to type
* 'GetInitialProps<{ subreddit: string; posts: readonly RedditPost[]; },
* NextContext<Record<string, string | string[] | undefined>, {}>>'.
*/
Posts.getInitialProps = getInitialProps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment