Skip to content
Merged
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
6 changes: 6 additions & 0 deletions packages/@cdklabs/typewriter/src/renderer/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ export class TypeScriptRenderer extends Renderer {
this.renderDocs(structType, { forceStruct: true });
this.emit(`${modifiers}interface ${structType.name}`);
this.renderTypeParameters(structType.typeParameters);

if (structType.extends.length > 0) {
this.emit(' extends ');
this.emitList(structType.extends, ', ', (t) => this.renderType(t));
}

this.emitBlock(' ', () => {
const props = Array.from(structType.properties.values()).filter((p) => p.visibility === MemberVisibility.Public);

Expand Down
4 changes: 4 additions & 0 deletions packages/@cdklabs/typewriter/src/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class StructType extends MemberType {
spec.properties?.forEach((p) => this.addProperty(p));
}

public get extends() {
return this.spec.extends ?? [];
}

/**
* Adds a property to the interface
*
Expand Down
36 changes: 35 additions & 1 deletion packages/@cdklabs/typewriter/test/interface.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InterfaceType, Module, TypeScriptRenderer } from '../src';
import { InterfaceType, Module, StructType, TypeScriptRenderer } from '../src';

const renderer = new TypeScriptRenderer();
let scope: Module;
Expand All @@ -24,3 +24,37 @@ test('can update some interface spec fields after initial creation', () => {
}"
`);
});

test('struct can extend struct', () => {
new StructType(scope, {
name: 'MyStruct',
extends: [scope.type('AnotherStruct')],
});

expect(renderer.render(scope)).toMatchInlineSnapshot(`
"/* eslint-disable prettier/prettier, @stylistic/max-len */
/**
* @struct
*/
interface MyStruct extends AnotherStruct {

}"
`);
});

test('struct can extend multiple structs', () => {
new StructType(scope, {
name: 'MyStruct',
extends: [scope.type('AnotherStruct'), scope.type('SecondStruct')],
});

expect(renderer.render(scope)).toMatchInlineSnapshot(`
"/* eslint-disable prettier/prettier, @stylistic/max-len */
/**
* @struct
*/
interface MyStruct extends AnotherStruct, SecondStruct {

}"
`);
});