Skip to content

Commit

Permalink
chore: added additional documentation around generics and hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseagle committed Jun 6, 2024
1 parent 078bba9 commit 55428d0
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion docs/documentation/08-Guides/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ return addHookData<MyCustomPropsType>(hookData, {
});
```

### Extending Framework Hooks

Certain hooks in the framework can accept Generic type parameters. This can used to provide a better developer experience during implementation as you will have clarity on the return type. It may often be required to extend WordPress REST API endpoints to return additional data to align with project requirements. As projects grow, you may need to implement this at hook level to return a specific subset of data as opposed to at route level within `getServerSideProps` or `getStaticProps`. An example is provided below.

```ts title="useExampleHook.ts"
interface CustomAppSettings extends AppEntity {
settings: AppEntity['settings'] & {
custom_settings: {
banner_text?: string;
};
};
}
const useExampleHook = () => {
const { data, error, loading } = useAppSettings<CustomAppSettings>();
...
}
```

## Recommended TS Config

This is the recommended TS Config for working with HeadstartWP and TypeScript
Expand Down Expand Up @@ -152,4 +170,4 @@ interface Window {

## Running the typecheck

By default Next.js will run `tsc` to validate your types. If type checking fails your build will fail. Therefore we recommend running `tsc --noEmit` before commiting and/or on your CI prior to merging PRs.
By default Next.js will run `tsc` to validate your types. If type checking fails your build will fail. Therefore we recommend running `tsc --noEmit` before commiting and/or on your CI prior to merging PRs.

0 comments on commit 55428d0

Please sign in to comment.