Skip to content

vue-tsc doesn't preserve JSDoc comments for emits in generated .d.ts files #5348

@happyyangyuan

Description

@happyyangyuan

Description:
When using defineEmits with TypeScript type syntax and JSDoc comments, the emitted .d.ts files discard all JSDoc documentation for emits while correctly preserving them for props. This significantly impacts developer experience as emit events lose their documentation in the generated type definitions.

Environment:

  • Vue: 3.5.13
  • vue-tsc: 5.8.3
  • Vite: 6.3.4
  • TypeScript: 5.8.3

Reproduction Steps:

  1. Create a component with documented emits:
<script setup lang="ts">
const emit = defineEmits<{
  /**
   * Triggered when operation succeeds
   * @event confirm-success
   */
  'confirm-success': [];
  /**
   * Triggered when operation fails
   * @event confirm-error
   * @param {Error} error - Detailed error information
   */
  'confirm-error': [error: Error];
}>();
</script>
  1. Run vue-tsc --declaration --emitDeclarationOnly

  2. Observe generated .d.ts file:

declare const __default: DefineComponent<{}, {}, {}, {}, {}, {}, {}, {
  'confirm-success': [];
  'confirm-error': [error: Error];
}>;

(Notice JSDoc is missing)

Proposed Solution

The solution would involve modifying vue-tsc's type declaration emitter to preserve JSDoc comments for defineEmits types, similar to how it already works for defineProps.

Technical Implementation Expectations:

  1. TypeScript AST Handling
    The emitter should retain JSDoc nodes attached to emit event signatures in the TypeScript AST when generating .d.ts files.

  2. Output Format
    The generated declarations should mirror the input's documentation structure:

    declare const __default: DefineComponent<{}, {}, {}, {}, {}, {}, {}, {
      /**
       * Triggered when operation succeeds
       * @event confirm-success
       */
      'confirm-success': [];
      /**
       * Triggered when operation fails
       * @event confirm-error
       * @param {Error} error - Detailed error information
       */
      'confirm-error': [error: Error];
    }>;
  3. Consistency with Props
    The behavior should match the existing JSDoc preservation for defineProps (which already works correctly).


Additional Notes

  1. Priority Areas
    The fix should prioritize:

    • Emits defined via type literals (defineEmits<{...}>())
    • Both event descriptions (/** comment */) and @param tags
  2. Backward Compatibility
    This change would be non-breaking, as it only adds documentation without affecting runtime behavior.

  3. Testing Suggestions
    Test cases should verify:

    • Simple event descriptions
    • Parameter-specific docs (@param)
    • Edge cases (union types, complex payloads)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions