-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
docs(tutorials/blog): add TS 4.9's satisfies
in combination with LoaderFunction
#4675
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
264004d
Sign CLA
tom-sherman f302b09
Add LoaderFunction type
tom-sherman 0406376
Mention this in the docs
tom-sherman ef22216
Revert routeModules
tom-sherman 32a5a4f
Update to latest version of the doc
tom-sherman 3008bf6
Update docs/tutorials/blog.md
tom-sherman 931ba33
Update docs/tutorials/blog.md
tom-sherman f858215
Merge branch 'dev' into loaderfunction-type
machour d8950d4
Merge branch 'dev' into loaderfunction-type
pcattori d466256
Merge branch 'dev' into loaderfunction-type
pcattori File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -511,6 +511,7 @@ | |
- tjefferson08 | ||
- tombiju | ||
- tombyrer | ||
- tom-sherman | ||
- TomerAberbach | ||
- tomer-yechiel | ||
- toyozaki | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -195,6 +195,34 @@ export default function Posts() { | |||||||||
|
||||||||||
Hey, that's pretty cool. We get a pretty solid degree of type safety even over a network request because it's all defined in the same file. Unless the network blows up while Remix fetches the data, you've got type safety in this component and its API (remember, the component is already its own API route). | ||||||||||
|
||||||||||
We can take the type safety a step further in TypeScript 4.9.0 using the `satisfies` keyword. This will ensure that we always return a type of `Response` from the loader instead of accidentally returning another type which would result in a runtime error. | ||||||||||
|
||||||||||
```ts filename=app/routes/posts/index.tsx lines=[1,23] | ||||||||||
import type { LoaderFunction } from "@remix-run/node"; | ||||||||||
import { json } from "@remix-run/node"; | ||||||||||
Comment on lines
+201
to
+202
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
import { Link, useLoaderData } from "@remix-run/react"; | ||||||||||
|
||||||||||
type Post = { | ||||||||||
slug: string; | ||||||||||
title: string; | ||||||||||
}; | ||||||||||
|
||||||||||
export const loader = (async () => { | ||||||||||
return json({ | ||||||||||
posts: [ | ||||||||||
{ | ||||||||||
slug: "my-first-post", | ||||||||||
title: "My First Post", | ||||||||||
}, | ||||||||||
{ | ||||||||||
slug: "90s-mixtape", | ||||||||||
title: "A Mixtape I Made Just For You", | ||||||||||
}, | ||||||||||
], | ||||||||||
}); | ||||||||||
}) satisfies LoaderFunction; | ||||||||||
``` | ||||||||||
|
||||||||||
## A little refactoring | ||||||||||
|
||||||||||
A solid practice is to create a module that deals with a particular concern. In our case it's going to be reading and writing posts. Let's set that up now and add a `getPosts` export to our module. | ||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.