Skip to content

Commit

Permalink
Add readonly option for TypeScript Language (#2534)
Browse files Browse the repository at this point in the history
  • Loading branch information
inferrinizzard authored Mar 31, 2024
1 parent 0d974b4 commit a92aa33
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
16 changes: 13 additions & 3 deletions packages/quicktype-core/src/language/TypeScriptFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const tsFlowOptions = Object.assign({}, javaScriptOptions, {
"prefer-const-values",
"Use string instead of enum for string enums with single value",
false
)
),
readonly: new BooleanOption("readonly", "Use readonly type members", false)
});

const tsFlowTypeAnnotations = {
Expand All @@ -52,7 +53,8 @@ export abstract class TypeScriptFlowBaseTargetLanguage extends JavaScriptTargetL
tsFlowOptions.rawType,
tsFlowOptions.preferUnions,
tsFlowOptions.preferTypes,
tsFlowOptions.preferConstValues
tsFlowOptions.preferConstValues,
tsFlowOptions.readonly
];
}

Expand Down Expand Up @@ -167,8 +169,16 @@ export abstract class TypeScriptFlowBaseRenderer extends JavaScriptRenderer {
protected emitClassBlockBody(c: ClassType): void {
this.emitPropertyTable(c, (name, _jsonName, p) => {
const t = p.type;

let propertyName: Sourcelike = name;
propertyName = modifySource(quotePropertyName, name);

if (this._tsFlowOptions.readonly) {
propertyName = modifySource(_propertyName => "readonly " + _propertyName, propertyName);
}

return [
[modifySource(quotePropertyName, name), p.isOptional ? "?" : "", ": "],
[propertyName, p.isOptional ? "?" : "", ": "],
[this.sourceFor(t).source, ";"]
];
});
Expand Down
3 changes: 2 additions & 1 deletion test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,8 @@ export const TypeScriptLanguage: Language = {
{ "declare-unions": "true" },
["pokedex.json", { "prefer-types": "true" }],
{ "acronym-style": "pascal" },
{ converters: "all-objects" }
{ converters: "all-objects" },
{ readonly: "true" }
],
sourceFiles: ["src/language/TypeScript.ts"]
};
Expand Down

0 comments on commit a92aa33

Please sign in to comment.