Skip to content

Add option for generate interface with readonly properties #131

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
ikokostya opened this issue Nov 14, 2017 · 1 comment
Open

Add option for generate interface with readonly properties #131

ikokostya opened this issue Nov 14, 2017 · 1 comment

Comments

@ikokostya
Copy link

ikokostya commented Nov 14, 2017

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.

@bcherny
Copy link
Owner

bcherny commented Nov 14, 2017

That's a really great idea @ikokostya! We might get requests to split it into two settings at some point (readOnlyProperties via readonly keyword, readOnlyValues via Readonly<> or ReadonlyArray<> types) - what do you think of splitting it that way?

Anyway, PRs are welcome!

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants