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
Function errors in typescript: has no properties in common with type 'Partial<PublicConfiguration<any, any, BareFetcher<any>>>' #2826
Comments
I've read through #939 and couldn't find an answer |
Weirdly, this stops the error... but... why!???? const { data, error, isValidating } = useSWR(
shouldFetch ? { query, sessionToken } : null,
getInfo,
{}, // <---- I added this
) |
These two issues are related #2822 This started happening from release v2.2.3 from the removal of a type in #2759. Adding this back to the SWRHook interface or reverting to a version before v2.2.3 address the type error you got.
Adding something ({} or undefined) after the fetcher makes the type fall onto one of the other overloads found in the SWRHook interface. |
@jarangutan thanks for your input. That was the 'fix' I used, although it's far from a fix — before I had proper types for my response, now, if I add any types it throws the error, and if I use |
Fully agree on it being far from a fix. My response was mostly to add context on where this goof started happening for others. And interesting :o I was getting one of the interfaces with What typescript version are you using? I wonder if that's making a difference if you're getting no types adding the config. |
tl; drtype QUERY_KEY = {
query: string;
sessionToken: string;
}
// ...
const fetcher: Fetcher<Result, QUERY_KEY> = (key) => fetch(...)
// ...
const { data, error, isValidating } = useSWR(
{ query, sessionToken } as QUERY_KEY, // <-- add the type assertion
fetcher,
) After adding type assertion, compiler stopped complaining and DetailI also faced this error when upgrading nodejs from I noticed that it only shows error when the key is NOT a literal type (
That's because you are trying to let the compiler thinks that you are overloading one of its "mutants". Try adding the |
This solves it, although I dislike type assertions. |
Ikr, it used to "just works". |
IMHO, that solution sounds more like a workaround, not a proper solution. I'd recommend leave it open. |
The SRW documentation says to do this (but it doesn't work)
While in reality in this way it works
with many params:
|
Bug report
Description / Observed Behavior
Updating the package, I have now some functions with typescript errors.
They throw
Type '({ query, sessionToken, }: Data) => Promise<ApiResponse>' has no properties in common with type 'Partial<PublicConfiguration<any, any, BareFetcher<any>>>'.
I am certainly doing something wrong, as some other functions work properly.
See Repro Steps for more details.
Expected Behavior
The function should not error or the docs should be clearer on how to set the correct types.
Repro Steps / Code Example
I tried with no luck
also::
If I add the function straight into the hook, no error is thrown:
If I cast the parameter as
any
it does not error anymore, e.g.:If I simplify it also doesn't work:
Just as a comparison: this works perfectly in my codebase:
Additional Context
SWR version:
"swr": "^2.2.4"
The text was updated successfully, but these errors were encountered: