Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: make input arrays readonly #236

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ function createMessageSignature(
field.wrapRepeatedType(
field.getType(fieldDescriptor, rootDescriptor),
fieldDescriptor,
true
),
),
fieldDescriptor.options?.deprecated,
Expand Down Expand Up @@ -672,7 +673,7 @@ function createPrimitiveMessageSignature(
)
? ts.factory.createToken(ts.SyntaxKind.QuestionToken)
: undefined,
field.wrapRepeatedType(fieldType as ts.TypeNode, fieldDescriptor),
field.wrapRepeatedType(fieldType as ts.TypeNode, fieldDescriptor, true),
),
);
}
Expand Down Expand Up @@ -1116,6 +1117,7 @@ function createSetter(
const type = field.wrapRepeatedType(
field.getType(fieldDescriptor, rootDescriptor),
fieldDescriptor,
true
);
const valueParameter = ts.factory.createIdentifier("value");

Expand Down
14 changes: 13 additions & 1 deletion src/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ import * as ts from "typescript";
/**
* @param {*} type
* @param {descriptor.FieldDescriptorProto} fieldDescriptor
* @param {boolean=} makeReadonly
*/
export function wrapRepeatedType(type: any, fieldDescriptor: descriptor.FieldDescriptorProto) {
export function wrapRepeatedType(
type: any,
fieldDescriptor: descriptor.FieldDescriptorProto,
makeReadonly = false
) {
if (isRepeated(fieldDescriptor) && !isMap(fieldDescriptor)) {
type = ts.factory.createArrayTypeNode(type);

if (makeReadonly) {
type = ts.factory.createTypeOperatorNode(
ts.SyntaxKind.ReadonlyKeyword,
type,
);
}
}

return type;
Expand Down