node-typescript-parser icon indicating copy to clipboard operation
node-typescript-parser copied to clipboard

type properties are not parsed

Open d9k opened this issue 2 years ago • 1 comments

script

import { TypescriptParser } from 'typescript-parser';

const json = (data: any): string => JSON.stringify(data, null, '  ');

async function main() {
    const timerToPreventFreeze = setTimeout(() => {}, 999999);
    const parser = new TypescriptParser();
    console.log('# interface');
    console.log();
    console.log(json(await parser.parseSource(`
        interface A {
            n: number;
            s: string;
        }
    `)));
    console.log();
    console.log('# type');
    console.log();
    console.log(json(await parser.parseSource(`
        type A = {
            n: number;
            s: string;
        }
    `)));
    clearTimeout(timerToPreventFreeze);
  }

  main().then(() => {
    process.exit()
  });

Result

# interface

{
  "filePath": "inline.tsx",
  "rootPath": "/",
  "start": 9,
  "end": 83,
  "imports": [],
  "exports": [],
  "declarations": [
    {
      "name": "A",
      "isExported": false,
      "start": 9,
      "end": 78,
      "accessors": [],
      "properties": [
        {
          "name": "n",
          "visibility": 2,
          "type": "number",
          "isOptional": false,
          "isStatic": false,
          "start": 35,
          "end": 45
        },
        {
          "name": "s",
          "visibility": 2,
          "type": "string",
          "isOptional": false,
          "isStatic": false,
          "start": 58,
          "end": 68
        }
      ],
      "methods": []
    }
  ],
  "resources": [],
  "usages": [
    "n",
    "s"
  ]
}

# type

{
  "filePath": "inline.tsx",
  "rootPath": "/",
  "start": 9,
  "end": 80,
  "imports": [],
  "exports": [],
  "declarations": [
    {
      "name": "A",
      "isExported": false,
      "start": 9,
      "end": 75
    }
  ],
  "resources": [],
  "usages": [
    "n",
    "s"
  ]
}

d9k avatar Dec 06 '23 10:12 d9k

Also no nested type declarations supported

d9k avatar Dec 06 '23 10:12 d9k