bcherny/json-schema-to-typescript
View on GitHubAdd option for generate interface with readonly properties
Open
#131 opened on Nov 14, 2017
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:
- https://github.com/Microsoft/TypeScript/issues/10725
- https://github.com/Microsoft/TypeScript/issues/13923
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.