Skip to content
The React Framework
JavaScript TypeScript Other
Branch: canary
Clone or download

Latest commit

manuschillerdev Add type inference for getStaticProps and getServerSideProps (#11842)
This adds `InferredStaticProps` and `InferredServerSideProps` to the typings.

- [x] add types for type inference 
- [x] add explanation to docs
- [ ] tests - are there any?

![inferred-props](https://user-images.githubusercontent.com/56154253/79068041-24bcab00-7cc4-11ea-8397-ed1b95fbeca7.gif)

### What does it do:

As an alternative to declaring your Types manually with:
```typescript
type Props = {
  posts: Post[]
}

export const getStaticProps: GetStaticProps<Props> = () => ({
  posts: await fetchMyPosts(),
})

export const MyComponent(props: Props) =>(
 // ...
);
```

we can now also infer the prop types with
```typescript
export const getStaticProps = () => ({
  // given fetchMyPosts() returns type Post[]
  posts: await fetchMyPosts(),
})

export const MyComponent(props: InferredStaticProps<typeof getStaticProps>) =>(
 // props.posts will be of type Post[]
);

```

### help / review wanted
- [ ] I am no typescript expert. Although the solution works as intended for me, someone with more knowledge could probably improve the types. Any edge cases I missed?
- [ ] are there any tests I should modify/ add?
Latest commit 9f1b4f5 May 27, 2020

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Update PR stats action branch check (#13394) May 26, 2020
.vscode skip node internals (#13345) May 25, 2020
bench Upgrade to Prettier 2 (#13061) May 18, 2020
docs Add type inference for getStaticProps and getServerSideProps (#11842) May 27, 2020
errors Add warning when reserved pages are nested (#13449) May 27, 2020
examples Add release argument to SentryWebpackPlugin (#13459) May 27, 2020
packages Add type inference for getStaticProps and getServerSideProps (#11842) May 27, 2020
test Add type inference for getStaticProps and getServerSideProps (#11842) May 27, 2020
.eslintignore [examples] with-ts-eslint-jest example app (#12025) May 25, 2020
.eslintrc.json Enable jest/no-try-expect (#13124) May 20, 2020
.gitignore Migrate PR stats action into Next.js repo (#13177) May 21, 2020
.npmrc Remove version prefix Oct 1, 2018
.prettierignore Migrate PR stats action into Next.js repo (#13177) May 21, 2020
.prettierignore_staged Upgrade to Prettier 2 (#13061) May 18, 2020
.prettierrc.json Update Prettier configuration Apr 26, 2019
CODE_OF_CONDUCT.md Update Code of Conduct email address (#12108) Apr 22, 2020
UPGRADING.md Move upgrading guide to /docs (#10727) Feb 28, 2020
azure-pipelines.yml Update to track test timings separate for Azure and Actions (#12083) Apr 21, 2020
check-pre-compiled.sh Pre-ncc compiled packages to prevent re-nccing unnecessarily (#11569) Apr 7, 2020
contributing.md Update codeload and deploy links for org rename (#13141) May 27, 2020
jest.config.js Use the jest-circus test runner (#12974) May 16, 2020
lerna.json v9.4.3-canary.2 May 27, 2020
license.md Update year on license files Apr 21, 2020
lint-staged.config.js Upgrade to Prettier 2 (#13061) May 18, 2020
package.json Migrate PR stats action into Next.js repo (#13177) May 21, 2020
publish-release.sh Make sure to exit publish script with correct code (#10310) Jan 28, 2020
readme.md Monorepo (#5341) Sep 30, 2018
run-tests.js Upgrade to Prettier 2 (#13061) May 18, 2020
test-file.txt Add additional file serving tests (#12479) May 4, 2020
yarn.lock Do not throw away `tsconfig.json` comments (#13458) May 27, 2020

readme.md

You can’t perform that action at this time.