Skip to content

Commit

Permalink
fix: useLink type overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
victorgarciaesgi committed Dec 6, 2023
1 parent f01256f commit 56504d7
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 13 deletions.
8 changes: 7 additions & 1 deletion docs/content/2.usage/1.NuxtLink.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ You can use it like you used it before.

```vue
<template>
<NuxtLink :to="`parent/child/two/profile/${id}`">Go to profile</NuxtLink>
<NuxtLink :to="`/parent/child/two/profile/${id}`">Go to profile</NuxtLink>
<NuxtLink :to="{
name: 'parent-child-two-profile-id',
params: { id: 1 }
Expand All @@ -26,6 +26,12 @@ You can do CLI type checking for `<NuxtLink/>` with the [`vue-tsc` package](http
`external` prop is supported since `v3.1.0`
::

```vue
<template>
<!-- Valid -->
<NuxtLink to="http://google.com" external>Go to profile</NuxtLink>
</template>
```

---

Expand Down
16 changes: 16 additions & 0 deletions docs/content/2.usage/2.useRoute.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,19 @@ console.log(route.params.id) // types-check
</script>
```



## `useLink`

Typings for `useLink` follow the usage of other composables.



```vue
<script setup lang='ts'>
const link = useLink({to: '/profile/999'});
</script>
```
17 changes: 17 additions & 0 deletions docs/content/2.usage/5.i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,20 @@ if (route) {
}

```


## `NuxtLinkLocale`

The component provived by `@nuxtjs/i18n` is also supported

```vue
<template>
<NuxtLinkLocale :to="`/parent/child/two/profile/${id}`" locale='fr'>Go to profile</NuxtLinkLocale>
<NuxtLinkLocale :to="{
name: 'parent-child-two-profile-id',
params: { id: 1 }
}">
Go to profile
</NuxtLinkLocale>
</template>
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"tsd": "0.29.0",
"typescript": "5.3.2",
"vitest": "0.34.6",
"vue": "3.3.10",
"vue-eslint-parser": "9.3.2",
"vue-router": "4.2.5",
"vue-tsc": "1.8.22",
Expand Down
6 changes: 4 additions & 2 deletions playground/src/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import TestLink from '~/components/TestLink.vue';
definePageMeta({
name: 'foo-bar',
redirect: { name: 'admin-id', params: { id: 1 } },
redirect: { name: 'admin-444', params: { '444': 1 } },
});
const router = useRouter();
Expand All @@ -50,11 +50,13 @@ function navigate() {
router.push(localePath('/admin/888'));
const link = useLink({ to: '/admin/37673' });
const u = 'krzfzlkj' as string;
const t = '///';
const route2 = localePath(`/user/${t}/:slug/articles`); // Should error
navigateTo('/admin/foo'); // Should error
navigateTo('/foo'); // Should error
router.push('/admin'); // Should error
const route = localePath(`/user/${u}/:slug/articles`);
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 19 additions & 10 deletions src/core/output/generators/files/__useTypedLink.file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function createUseTypedLinkFile(): string {
return /* typescript */ `
import { useLink as defaultLink } from '#imports';
import type {MaybeRef} from 'vue';
import type {MaybeRef, Ref} from 'vue';
import type { NavigateToOptions } from 'nuxt/dist/app/composables/router';
import type { NavigationFailure } from 'vue-router';
import type { TypedRouteLocationRawFromName, TypedRouteFromName, TypedRoute } from './__router';
Expand All @@ -31,20 +31,29 @@ export function createUseTypedLinkFile(): string {
interface UseLinkFunction {
<T extends RoutesNamesList, P extends string>(
props: {
to: MaybeRef<TypedRouteLocationRawFromName<T, P>>,
replace?: MaybeRef<boolean>
}
) : LinkedRoute<T>
<T extends RoutesNamesList, P extends string>(props: {
to: TypedRouteLocationRawFromName<T, P>;
replace?: MaybeRef<boolean>;
}): LinkedRoute<T>;
<T extends RoutesNamesList, P extends string>(props: {
to: Ref<TypedRouteLocationRawFromName<T, P>>;
replace?: MaybeRef<boolean>;
}): LinkedRoute<T>;
${returnIfTrue(
pathCheck && !strictOptions.router.strictToArgument,
`<T extends string>(
`<P extends string>(
props: {
to: MaybeRef<TypedPathParameter<T>>,
to: TypedPathParameter<P>,
replace?: MaybeRef<boolean>
}
) : LinkedRoute<RouteNameFromPath<T>>`
) : LinkedRoute<RouteNameFromPath<P>>
<P extends string>(props: {
to: Ref<TypedPathParameter<P>>;
replace?: MaybeRef<boolean>;
}): LinkedRoute<RouteNameFromPath<P>>;
`
)}
}
Expand Down

0 comments on commit 56504d7

Please sign in to comment.