bcherny/json-schema-to-typescript

Add option for generate interface with readonly properties

Open

#131 opened on Nov 14, 2017

View on GitHub
 (1 comment) (12 reactions) (0 assignees)TypeScript (449 forks)github user discovery
enhancementgood first issuehelp wanted

Repository metrics

Stars
 (3,302 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

The problem

I want to generate interface for object which must not be changed. I can manually wrap result using DeepReadonly type:

type DeepReadonly = {
   readonly [P in keyof T]: DeepReadonly<T[P]>;
};

but it doesn't work with arrays:

Suggestion

Add some option to json-schema-to-typescript for generating readonly properties. For example from README.md:

export interface ExampleSchema {
  readonly firstName: string;
  readonly lastName: string;
  readonly age?: number;
  readonly hairColor?: "black" | "brown" | "blue";
}

For arrays ReadonlyArray type can be used:

export interface ExampleSchema {
    readonly arr: ReadonlyArray<string>
}

Questions

Unfortunately there is no way to create read only tuple (as I known) if json-schema-to-typescript produces it.

Contributor guide