最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论