Typed links #1026
Replies: 6 comments 18 replies
-
I'd be +1 for typing
|
Beta Was this translation helpful? Give feedback.
-
Other thing that has been proposed, using template literals: type Link = '/' | `/blog/${string}` And then extend astro/framework types for |
Beta Was this translation helpful? Give feedback.
-
I wonder if a type helper could also be nice, e.g. I have a |
Beta Was this translation helpful? Give feedback.
-
How about typing links with template literal types instead, so each route would have a type generated such as type TypedLink = `/blog/${string}` You could then type links with a union of those types, as well as the route pattern itself so it would show in autocomplete. I think you may need some trickery to avoid collapsing the types: type TypedLink = `/blog/[...slug]` | (`/blog/${string}` & {} ) | `/users/[id]` | ( `/users/${string}` & {});` We could then update the types for |
Beta Was this translation helpful? Give feedback.
-
Some of us already know this, but many don't. I think you missed the part that explains which part of the suggested API is typed 😅 |
Beta Was this translation helpful? Give feedback.
-
Given all the things that have been shared in the threads/comments, I still think a little runtime function is better than only a template literal. We can still expose a type for libraries to consume. What do you think? |
Beta Was this translation helpful? Give feedback.
-
Body
Summary
Have a way to typecheck links.
Background & Motivation
It's easy to make mistakes when writing links: you can forget to take the base or the trailing slash into consideration, or even just mispell a route.
It would be useful to have a shorter feedback look during development and be able to cancel a build if you made a mistake.
Goals
Non goals
Example
Other frameworks like Nuxt or Next.js have this feature in experimental, through a feature flag:
That makes routing APIs typed, such as their
<Link />
component ornavigate()
helpers.I think Astro is in a different situation: we don't have such helpers. Want a link? Just use
<a />
. Want to navigate? UseAstro.redirect()
,window.location.push()
ornavigate()
. What I mean is that most of these APIs are not exclusively for routes owned by Astro.We could type all those cases to be strictly typed but not fail if it's not strictly correct to account for situations where you want to point to external URLs, but defeats the whole purpose of this proposal. That would also be hard since we support many frameworks/syntaxes.
Instead, I suggest we follow the same approach as https://github.com/florian-lefebvre/astro-typed-links: we provide a helper function that is typed. It's much simpler code-wise and it allows users to opt-in instead of messing up with standard (eg. typing
a
href
).Until stable, it would be enabled with an experimental flag:
The link function would be made available through the
astro:link
virtual module. Here are some example usages:IMO an advantage of this approach is that it's agnostic: you can use it in Astro files, React, on the backend for webhooks redirect urls etc...
Note this approach allows to type any Astro route, including injected ones. That means it's fully compatible with integrations such as https://inox-tools.fryuni.dev/custom-routing by @Fryuni.
There's a tentative implementation PR at withastro/astro#12041
Beta Was this translation helpful? Give feedback.
All reactions