json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

Generated d.ts file doesn't create references to schema correctly

Open kbfirebreather opened this issue 3 years ago • 3 comments

My schema is pretty large, but I whittled it down to what's attached that seems to generate the issue.

My code is as follows:

import { compileFromFile } from 'json-schema-to-typescript';

...

const ts = await compileFromFile(file);
const name = path.basename(file, '.json');
await writeFile(`${outDir}/${name}.d.ts`, ts);

Where file is the .json file.

Attached is typescript-demo-parent.json which is the input file that gets passed to compileFromFile, and the resulting output typescript-demo-parent.d.ts. In the output, results->matches is referenced as Matches1[]. From the exports, Matches1 is a ScoredItem, where as Matches is Matches1 & Matches2. Matches2 has the id required property that is defined in the json file. From my point of view, results->matches should really be Matches[] rather than Matches1[]. When I enforce linting against this object in code, and try to set .id, it complains that it's not a valid property.

I uploaded the files with the .log extension so github would accept me uploading them. Remove the extension to reproduce the issue.

typescript-demo-parent.json.log

typescript-demo-parent.d.ts.log

kbfirebreather avatar Oct 07 '22 19:10 kbfirebreather

I believe this is a regression from version 10. I was using 11.0.2 and had the same problem. After I downgraded to 10.1.4 there was no more duplication of referenced schemas.

q-stefanorzu avatar Oct 21 '22 11:10 q-stefanorzu

Thanks for the tip @q-stefanorzu. My schema is unfortunately using newer syntax that was made compatible in version 11, so attempting to compile on Version 10 makes things even worse.

kbfirebreather avatar Nov 08 '22 03:11 kbfirebreather

Thanks for the report. It's a little hard to see what exactly the issue is -- would you mind whittling your repro case down even more (eg. removing unneccessary properties like $id, $schema, description, required, patternProperties, and anything else that's not needed), then pasting your schema, output, and expected output using code blocks in a comment? Thanks.

bcherny avatar Jan 21 '23 05:01 bcherny