javascript - How to omit exports of interfaces generated by json2ts (json-schema-to-typescript)? - Stack Overflow

admin2025-04-17  2

The way things work in VSCode is that you can create a something.d.ts file and as long as that file does neither import nor export anything, it is considered globally defined within the project.

This is very convenient for augmenting plain JavaScript with advanced type definitions for things such as API responses.

If a .d.ts file does use export however, you are required to import any symbols you want to use. For things like API responses, there is no need to do that as the definitions do not do anything in plain JS. For example, I'd have a file my_response.d.ts:

interface MyResponse {
    name: string;
    someNumber: number;
}

Then in plain JS I can do this:

/** @type {MyResponse} **/
const parsed = JSON.parse(messageString);

And IDE would then hint the fields of MyResponse on that variable.

I generated a .d.ts file using json2ts like this:

json2ts --input ../myschemas/messages.schema.json --output generated_defs/messages_schema.d.ts

But the output has exports for the symbols.

export interface MessagesSchema {
  messages: Message[];
  [k: string]: unknown;
}
export interface Message {
  name: string;
  id: number;
  data: MessageField[];
  [k: string]: unknown;
}

I would like to tell the generator to omit those in this case, so I can use the symbols across the entire project without having to import them. How to do that?

转载请注明原文地址:http://anycun.com/QandA/1744826523a88156.html