mefechoel / svelte-navigator

Simple, accessible routing for Svelte
Other
506 stars 39 forks source link

TypeError useResolve "Expected 1 arguments, but got 2" #78

Closed tofsjonas closed 2 years ago

tofsjonas commented 2 years ago

If I do this, like in the example here: useResolve

import { link, useResolve, useLocation } from "svelte-navigator";

export let path;

const resolve = useResolve();
const location = useLocation();
// Force Svelte to re-run this assignement, when location changes
$: resolvedLink = resolve(path, $location);

I get this error in VSCode:

let $location: NavigatorLocation<AnyObject>
Expected 1 arguments, but got 2.  ts(2554)

(Also, path is empty, so it doesn't work unless I hardcode it, which makes the logic pointless 😢)

mefechoel commented 2 years ago

Yes, the second parameter is kind of a hack to force svelte to update the resolved link when the location changes. You can try casting the second argument to never to make ts happy. Otherwise this will work, but is slightly more complex:

let resolvedLink;
$: {
  if ($location) {
    resolvedLink = resolve(path);
  }
}

The if condition is always truthy, but again forces svelte to re run the resolve function when the location changes.

tofsjonas commented 2 years ago

That works, thanks! 😊