Skip to content

Commit 16b9529

Browse files
Fix Array used instead of ReadonlyArray for input type when immutableTypes config is on (dotansimha#10806)
* Fix Array used instead of ReadonlyArray for input type when immutableTypes config is on * Add changeset --------- Co-authored-by: Eddy Nguyen <ch@eddeee888.me>
1 parent df7a610 commit 16b9529

3 files changed

Lines changed: 11 additions & 5 deletions

File tree

.changeset/four-cameras-enter.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/typescript-operations': patch
3+
---
4+
5+
Fix Array used instead of ReadonlyArray while immutableTypes config is true

packages/plugins/typescript/operations/src/visitor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
327327
}
328328

329329
if (typeNode.type === 'ListType') {
330-
typePart = `Array<${typePart}>`;
330+
const listModifier = this.config.immutableTypes ? 'ReadonlyArray' : 'Array';
331+
typePart = `${listModifier}<${typePart}>`;
331332
if (!typeNode.isNonNullable) {
332333
typePart = printTypeScriptMaybeType({
333334
type: typePart,

packages/plugins/typescript/operations/tests/ts-documents.standalone.input.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ describe('TypeScript Operations Plugin - Input', () => {
241241
readonly to?: Date | null | undefined;
242242
readonly timezone?: unknown;
243243
readonly role?: UserRole | null | undefined;
244-
readonly ageRange1?: Array<number | null | undefined> | null | undefined;
245-
readonly ageRange2: Array<number | null | undefined>;
246-
readonly ageRange3?: Array<number> | null | undefined;
247-
readonly ageRange4: Array<number>;
244+
readonly ageRange1?: ReadonlyArray<number | null | undefined> | null | undefined;
245+
readonly ageRange2: ReadonlyArray<number | null | undefined>;
246+
readonly ageRange3?: ReadonlyArray<number> | null | undefined;
247+
readonly ageRange4: ReadonlyArray<number>;
248248
readonly bestFriend?: UsersBestFriendInput | null | undefined;
249249
readonly nestedInput?: UsersInput | null | undefined;
250250
};

0 commit comments

Comments
 (0)