Fix TypeScript errors in "Configuration" & "Routing and navigation" code snippets#14089
Fix TypeScript errors in "Configuration" & "Routing and navigation" code snippets#14089ArmandPhilippot wants to merge 22 commits into
Conversation
Lunaria Status Overview🌕 This pull request will trigger status changes. Learn moreBy default, every PR changing files present in the Lunaria configuration's You can change this by adding one of the keywords present in the Tracked Files
Warnings reference
|
|
Preview deployment ✅ Deployment complete!
|
|
This is now focused on "Configuration" & "Routing and navigation" code snippets, follow-up PRs:
Well, the PR is smaller but the description is now bigger. 😅 Feel free to ask for more details! |
ae57043 to
a904919
Compare
Description (required)
Fixes TypeScript errors in "Configuration" and "Routing and navigation" pages:
view-transitions.mdx:Astro.propsquerySelectorcan returnnullevent.target.valueis not available without narrowing the typeredirectcan be null when usingparams.get<script />tagsetupStuff()comes from nowhere, a fake import can be helpfulprefetch.mdxdocument.getElementByIdcan benullforEachdoesn't work withHTMLCollectionOfgetAttributecan returnnullinternationalization.mdxctx.urlis aURL,startsWithis not availableredirectToDefaultLocale(we should passctx)middleware.mdxcontext.beforelocalsendpoints.mdxparams.idisstring | undefinedandundefined"cannot be used as an index type". If we narrow the type tostring(usingAPIRoutetype parameters), TypeScript complains because a string can't be used for index access...Number()might not be the best (NaN) but it's the easier fix to make it TS friendly.satisfies APIRoute, TypeScript complains about the destructured properties.Bufferand, unlikeprocess.env, there is no clues in the editor to fix this, adding a comment might be helpfulrouting.mdx:pagewithoutsatisfies GetStaticPathsfunction getStaticPathshas been updated toconst getStaticPathsabove, the following code snippet should use the same syntaxpost.frontmatter.tag, we needpost: anytypescript.mdx:as Paramswhensatisfies GetStaticPathsis used is misleading: we don't need a cast hereTo help review and explain my changes I made a commit per code snippet (with a few exceptions where the changes were the same), with a short explanation of why in the commit message. But, I also repeat the explanation above. Choose what is easier for you!
I haven't updated a few code snippets in
view-transitions.mdxas I'm not really familiar with the API but:<title>seems weird to mediff()in anis:inlinescript... not sure this is clear to everyone wherediff()comes from.If you're interested in the "why," here's my reasoning:
Code snippets should work with minimal friction for users: TypeScript red squiggles or missing imports provide bad DX, especially for people new with web dev. I guess this can help AI too now.
Also, since Astro v5, the default Typescript preset is
strictinstead ofbase. This means TypeScript is now reporting errors for some code snippets that could work before. We had some reports before, we fixed them. We still get new reports from time to time, so I'm starting to check all our code snippets.re: formatting changes
In the core repo they tend to use single quotes, but in a new project without custom configuration (in VSCode, with the Astro extension enabled), this is automatically formatted with double quotes: I think minimal visual changes for the user are a good thing. So, I haven't undone the formatting changes made when correcting the code snippets.
kinda related to #10073
Related issues & labels (optional)