bcherny / json-schema-to-typescript

Compile JSON Schema to TypeScript type declarations
https://bcherny.github.io/json-schema-to-typescript-browser/
MIT License
2.96k stars 393 forks source link

Genereate .ts files instead of .d.ts files. #371

Open B3Kay opened 3 years ago

B3Kay commented 3 years ago

I would love to generate myTypes.ts instead of generating myTypes.d.ts Is this possible? "build:types": "yarn json2ts -i src/feature/schema/ -o src/feature/type/

ValeryR commented 3 years ago

@B3Kay, hello, i am not a author, but i can suggest solution 🙂 i had the same problem. you can write small script around json2ts which will generate and write .ts instead of .d.ts like this:

const json2ts = require('json-schema-to-typescript');
const fs = require('fs');

const inputData = {
  "title": "Example Schema",
  "type": "object",
  "properties": {
    "lastName": {
      "title": "Бла-бла-бла",
      "type": "string"
    },
  },
  "additionalProperties": false,
  "required": ["firstName", "lastName"]
};

(async function example() {
  const compiledModels = await json2ts.compile(inputData, '');
  fs.writeFileSync(`output.ts`, compiledModels);
})();

and if you add cli parameters to the script it will be work fine

bcherny commented 3 years ago

Hey, curious if you could talk about the motivation for this a bit. Would love to understand your use case better.

thoughtentity commented 3 years ago

Came across this issue recently. The issue with only supporting d.ts output is that the generated files can't be included in the output of a typescript build. As described by a member of the Typescript dev team: "The .d.ts files are considered "references" the compiler will not touch them, not move them, or recreate them".

This becomes an issue if you want to bundle the generated code in an npm package to share with various projects as you can't just re-export the interfaces in the d.ts files from the package because the d.ts files while present in your source will NOT be in the compiled build output from typescript. You would need a separate build step to copy these files to the build output. If these were normal .ts files the typescript compiler would do this step for you.

rgrimball-olt commented 2 years ago

Maybe an option to generate each would be a good idea, or just generate both by default?

It isn't really a big deal for me since I just rename the file to .ts initially, dump that into my project, and then copy and paste the contents of the .d.ts file in subsequent runs into that .ts file in my project replacing everything there prior. But it is a bit of a cludge. Would be much nicer to just copy the file over. I don't want to have it explicitly part of my build process since when changes occur I want to be able to review them, but in theory I could do that too or as another has noted have the build process also just handle renaming the file. But again, I didn't want to actually do that. Just review the changes and overwrite the file if all looked good.

I spent forever trying to figure out how to get a d.ts file to be included in the build, but short of building a separate library to be exported to a NPM project and then imported back into the project solely for the benefit of getting this file, I couldn't find a way to do it that wasn't worse than just renaming it to a .ts file, and since we don't have an internal NPM repo, even that "more trouble than it is worth" solution wasn't feasible.