diff --git a/packages/type-compiler/src/compiler.ts b/packages/type-compiler/src/compiler.ts index 3b6d5a984..5bc9688d7 100644 --- a/packages/type-compiler/src/compiler.ts +++ b/packages/type-compiler/src/compiler.ts @@ -1778,6 +1778,10 @@ export class ReflectionTransformer implements CustomTransformer { ? ReflectionOp.method : ReflectionOp.function, program.findOrAddStackEntry(name), ); + if ((isMethodSignature(narrowed) || isMethodDeclaration(narrowed)) && narrowed.questionToken) { + program.pushOp(ReflectionOp.optional); + } + if (isMethodDeclaration(narrowed)) { if (hasModifier(narrowed, SyntaxKind.PrivateKeyword)) program.pushOp(ReflectionOp.private); if (hasModifier(narrowed, SyntaxKind.ProtectedKeyword)) program.pushOp(ReflectionOp.protected); diff --git a/packages/type/tests/compiler.spec.ts b/packages/type/tests/compiler.spec.ts index 6b73da084..e48af1494 100644 --- a/packages/type/tests/compiler.spec.ts +++ b/packages/type/tests/compiler.spec.ts @@ -644,6 +644,30 @@ test('emit function types in objects', () => { } as Type); }); +test('emit optional for method signatures', () => { + const code = ` + interface Wrap { + maybe?(item: string): any; + } + return typeOf(); + `; + const js = transpile(code); + console.log('js', js); + const type = transpileAndReturn(code); + expectEqualType(type, { + kind: ReflectionKind.objectLiteral, + types: [ + { + kind: ReflectionKind.methodSignature, + name: 'maybe', + optional: true, + parameters: [{ kind: ReflectionKind.parameter, name: 'item', type: { kind: ReflectionKind.string } }], + return: { kind: ReflectionKind.any } + } + ] + } as Type); +}); + test('emit class extends types', () => { const code = ` class ClassA { item: T; }