ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

Error when following exports?

Open WillieRuemmele opened this issue 9 months ago • 0 comments

The Salesforce CLI uses this package to ensure we don't break any of our --json output, which has been super useful, but I think I've found a bug in the library, I'm not sure where it is, but I think it's related to how a package defines exports in its package.json

scenario:

we've been using this, in a node script

      const config = {
        path: file,
        skipTypeCheck: true,
        type: returnType,
      }
      return createGenerator(config).createSchema(config.type)

to generate our schemas. I've been moving types from one repo to another, these types are similar, except for a few minor differences, like changing

export type CustomField = Metadata & {
    businessOwnerGroup?: string | null | undefined;
    businessOwnerUser?: string | null | undefined;
    businessStatus?: string | null | undefined;

to

export type CustomField = Metadata & {
    businessOwnerGroup?: string;
    businessOwnerUser?: string;

so changing the type, but in a way that shouldn't matter since it's already an optional property. We then Pick a few properties off of that object for our final return type (that we use the parser to ensure remains the same) like

type SaveableCustomField = Pick<
  CustomField,
  | 'label'
  | 'description'

everything compiles in typescript, but when we run the generator on this, we get an error

Error: Invalid index "label" in type "any"
    at /Users/william.ruemmele/projects/oclif/plugin-command-snapshot/node_modules/ts-json-schema-generator/src/NodeParser/IndexedAccessTypeNodeParser.ts:77:27
    at Array.map (<anonymous>)
    at IndexedAccessTypeNodeParser.createType (/Users/william.ruemmele/projects/oclif/plugin-command-snapshot/node_modules/ts-json-schema-generator/src/NodeParser/IndexedAccessTypeNodeParser.ts:62:42)
    at ChainNodeParser.createType (/Users/william.ruemmele/projects/oclif/plugin-command-snapshot/node_modules/ts-json-schema-generator/src/ChainNodeParser.ts:35:54)
    at /Users/william.ruemmele/projects/oclif/plugin-command-snapshot/node_modules/ts-json-schema-generator/src/NodeParser/MappedTypeNodeParser.ts:112:59
    at Array.reduce (<anonymous>)
    at MappedTypeNodeParser.getProperties (/Users/william.ruemmele/projects/oclif/plugin-command-snapshot/node_modules/ts-json-schema-generator/src/NodeParser/MappedTypeNodeParser.ts:111:14)
    at MappedTypeNodeParser.createType (/Users/william.ruemmele/projects/oclif/plugin-command-snapshot/node_modules/ts-json-schema-generator/src/NodeParser/MappedTypeNodeParser.ts:42:22)
    at ChainNodeParser.createType (/Users/william.ruemmele/projects/oclif/plugin-command-snapshot/node_modules/ts-json-schema-generator/src/ChainNodeParser.ts:35:54)
    at TypeAliasNodeParser.createType (/Users/william.ruemmele/projects/oclif/plugin-command-snapshot/node_modules/ts-json-schema-generator/src/NodeParser/TypeAliasNodeParser.ts:40:43)

after lots of debugging, replacing values in node_modules, even reverting all changes and only changing the type file I haven't found a way past it.

The one thing I've found that works, is if I import from @salesforce/types/lib/metadata.js instead of @salesforce/types/metadata (this is the new package I've been moving things into)

this repo has

  "exports": {
    "./apex": "./lib/apex.js",
    "./metadata": "./lib/metadata.js",
...

where as the original (working) repo, has no exports defined

WillieRuemmele avatar May 03 '24 21:05 WillieRuemmele