-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46043fb
commit 77a3e96
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { Equal, Expect } from "../helpers/type-utils"; | ||
|
||
interface User { | ||
fullName: string; | ||
job: string; | ||
} | ||
|
||
const getUser = async (): Promise<User> => { | ||
return Promise.resolve({ | ||
fullName: "Matt Pocock", | ||
job: "Developer", | ||
}); | ||
}; | ||
|
||
/** | ||
* A bit of a hint to start with. useQuery has a LOT of overloads - but a lot | ||
* of them are disappearing in the next major version. The ones that'll be | ||
* sticking around are the first three. | ||
* | ||
* 1. How are the first three overloads different to the others? Put differently, | ||
* what's the thing that's similar about the first three that's different to | ||
* the other six? | ||
*/ | ||
|
||
useQuery; | ||
// ^ CMD+click on this to see the overloads | ||
|
||
/** | ||
* 2. When you provide queryFn to useQuery, the type of query.data is inferred | ||
* as the return type of queryFn. Why is that? | ||
*/ | ||
const query1 = useQuery({ | ||
queryFn: getUser, | ||
}); | ||
|
||
// Without initialData, the type of query1.data is User | undefined | ||
type test1 = Expect<Equal<typeof query1.data, User | undefined>>; | ||
|
||
/** | ||
* 3. When you provide initialData to useQuery, the type of query.data no longer | ||
* has undefined in it. Why is that? | ||
*/ | ||
const query2 = useQuery({ | ||
queryFn: getUser, | ||
initialData: { | ||
fullName: "", | ||
job: "", | ||
}, | ||
}); | ||
|
||
// WITH initialData, the type of query.data is just User | ||
type test2 = Expect<Equal<typeof query2.data, User>>; |