Skip to content

docs: add jsdoc comments to plugin/generator types #4204

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

Open
wants to merge 3 commits into
base: alpha
Choose a base branch
from

Conversation

RMHonor
Copy link

@RMHonor RMHonor commented May 18, 2025

This PR adds JSDoc comments to the plugin/bundle config options, as discussed in #3790, giving usage hints inline and to hopefully pave the way for auto generated docs too.

The content for these docs were largely copied directly from the Router API docs with some tweaks to make it logical in the code context.

No logic changes have been made, purely type annotations.

Note about Zod

When adding JSDoc comments to fields with Zod, they are lost when using the z.input utility types (issue). To get around this, an interface is created, which is annotated with JSDoc, and then the accuracy of this is checked against again the schema with satisfies. This interface is then using for the type arguments in the plugin/generator.

A small drawback in this approach is that the compiler won't detect extraneous options being added to the schema.

There's some discrepancy with how input vs. output types are used here, but I believe that may be intentional? (see PR comments).

Example output:
Screenshot 2025-05-18 at 22 52 14

> = (options = {}) => {
let ROOT: string = process.cwd()

let userConfig = options as Config
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an unrelated point, this type casting here is risky. Config is the z.output type, which will contain defined properties due to the default option, but what's being passed it hasn't been parsed by Zod yet (and so is the z.input type).

For example, userConfig.routesDirectory may not have been defined yet, so the usage on L24-26 is unsafe.

There is some complexity here, because we can't immediately parse the provided schema, as there may be later config retrieved from a tsr.config.ts file (here), and the default values would override anything provided in the config file. This could be fixed by allowing the file config to override the inline config, if that is the desired hierarchy.

Copy link
Contributor

@zrosenbauer zrosenbauer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDK if tanstack team wants to add them but I'm all for it 👍

@@ -2,6 +2,192 @@ import path from 'node:path'
import { existsSync, readFileSync } from 'node:fs'
import { z } from 'zod'
import { virtualRootRouteSchema } from './filesystem/virtual/config'
import type { VirtualRootRoute } from '@tanstack/virtual-file-routes'

export interface ConfigOptions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

big fan of this, helps a lot when in middle of coding :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants