syavorsky / comment-parser

Generic JSDoc-like comment parser.
MIT License
239 stars 24 forks source link

How Do you import this from typescript and use it? #171

Open radiantone opened 5 months ago

radiantone commented 5 months ago

I tried many variations but nothing works

kristianmandrup commented 2 months ago

Try adding a comment-parser.d.ts file in. a @types folder in your root or src folder

Ensure that TypeScript is aware of this file by including its path in your tsconfig.json:

{
  "include": ["src/**/*", "@types/**/*"]
}

comment-parser.d.ts

declare module 'comment-parser' {
  import { ParsedComment } from 'comment-parser/lib/types'; // Adjust import path if necessary

  export function parse(source: string): ParsedComment[];
}

or

declare module 'comment-parser' {
  interface Tag {
    tag: string;
    name: string;
    type?: string;
    optional?: boolean;
    default?: string;
    description: string;
    problems: any[];
    source: string[];
  }

  interface ParsedComment {
    description: string;
    tags: Tag[];
  }

  export function parse(source: string): ParsedComment[];
}
brettz9 commented 2 months ago

Should be no need for a declaration file.

import {parse as commentParser} from 'comment-parser';

// See the structure yourself
console.log(
  commentParser(`
    /**
     * @param {someType} someName A description
     */
`)
);
kristianmandrup commented 2 months ago

I got it working with the above solution, but sometimes it is just a local TS server issue. I've seen that frequently with VS Code. Sometimes it can be solved via Cmd-P Restart TS Server, sometime with a full reboot of the IDE.