Skip to content

Commit 1aa2b7e

Browse files
authored
fix: Typegen handles structs and vectors of structs without erroring (#4743)
1 parent 677d0c9 commit 1aa2b7e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

packages/typegen/src/util/derived.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,21 @@ describe('getSimilarTypes', (): void => {
4141
'Vec<Vec<Balance>>'
4242
]);
4343
});
44+
45+
it('handles structs', (): void => {
46+
expect(getSimilarTypes(registry, {}, '{ "a": "u8", "b": "Vec<u8>" }', mockImports)).toEqual([
47+
`{
48+
readonly a: u8;
49+
readonly b: Bytes;
50+
} & Struct`, '{ a?: any; b?: any }', 'string', 'Uint8Array'
51+
]);
52+
});
53+
it('handles vectors of structs', (): void => {
54+
expect(getSimilarTypes(registry, {}, 'Vec<{ "a": "H256", "b": "Vec<H256>" }>', mockImports)).toEqual([
55+
`Vec<{
56+
readonly a: H256;
57+
readonly b: Vec<H256>;
58+
} & Struct>`
59+
]);
60+
});
4461
});

packages/typegen/src/util/derived.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ export function getSimilarTypes (registry: Registry, definitions: Record<string,
6060
possibleTypes.push(`([${subs.join(', ')}])[]`);
6161
} else if (subDef.info === TypeDefInfo.VecFixed) {
6262
// TODO: Add possibleTypes so imports work
63+
} else if (subDef.info === TypeDefInfo.Struct) {
64+
// TODO: Add possibleTypes so imports work
6365
} else {
6466
throw new Error(`Unhandled subtype in Vec, ${stringify(subDef)}`);
6567
}

0 commit comments

Comments
 (0)