comment-parser icon indicating copy to clipboard operation
comment-parser copied to clipboard

How Do you import this from typescript and use it?

Open radiantone opened this issue 1 year ago • 4 comments

I tried many variations but nothing works

radiantone avatar Jun 01 '24 15:06 radiantone

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[];
}

kristianmandrup avatar Aug 24 '24 18:08 kristianmandrup

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
     */
`)
);

brettz9 avatar Aug 25 '24 02:08 brettz9

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.

kristianmandrup avatar Aug 30 '24 05:08 kristianmandrup

Just added latest, it does not appear to export any type info:

Could not find a declaration file for module 'comment-parser'. 'c:/Users/rhysv/Projects/lua-doc-extractor/node_modules/comment-parser/lib/index.cjs' implicitly has an 'any' type.\n Try npm i --save-dev @types/comment-parser if it exists or add a new declaration (.d.ts) file containing declare module 'comment-parser';

Restarting TS server did not work, but also I haven't had such a problem with any other package.

It's strange because I can see the type definitions in the module folder, but VSCode doesn't seem to want to use them.

UPDATE: I ended up writing my own type definition using the example in the readme. Would love to use the exported types, but they simple don't resolve.

You can see yourself here: https://github.com/rhys-vdw/lua-doc-extractor/tree/d6676877e5e1d9802ddb832941b998b14cf02400

rhys-vdw avatar Nov 16 '24 10:11 rhys-vdw