diff --git a/src/transformer/descriptor/descriptor.ts b/src/transformer/descriptor/descriptor.ts index 0986c3427..32500ee79 100644 --- a/src/transformer/descriptor/descriptor.ts +++ b/src/transformer/descriptor/descriptor.ts @@ -25,6 +25,7 @@ import { GetObjectLiteralDescriptor } from './objectLiteral/objectLiteral'; import { GetPropertyDescriptor } from './property/propertySignature'; import { GetStringDescriptor } from './string/string'; import { GetTypeAliasDescriptor } from './typeAlias/typeAlias'; +import { GetTypeLiteralDescriptor } from './typeLiteral/typeLiteral'; import { GetTypeParameterDescriptor } from './typeParameter/typeParameter'; import { GetTypeReferenceDescriptorReusable } from './typeReference/typeReferenceReusable'; import { GetUndefinedDescriptor } from './undefined/undefined'; @@ -37,6 +38,7 @@ export function GetDescriptor(node: ts.Node, scope: Scope): ts.Expression { case ts.SyntaxKind.TypeReference: return GetTypeReferenceDescriptorReusable(node as ts.TypeReferenceNode, scope); case ts.SyntaxKind.TypeLiteral: + return GetTypeLiteralDescriptor(node as ts.TypeLiteralNode, scope); case ts.SyntaxKind.InterfaceDeclaration: return GetInterfaceDeclarationDescriptor(node as ts.InterfaceDeclaration, scope); case ts.SyntaxKind.ClassDeclaration: diff --git a/src/transformer/descriptor/typeLiteral/typeLiteral.ts b/src/transformer/descriptor/typeLiteral/typeLiteral.ts new file mode 100644 index 000000000..ecd926e66 --- /dev/null +++ b/src/transformer/descriptor/typeLiteral/typeLiteral.ts @@ -0,0 +1,14 @@ +import * as ts from 'typescript'; +import { Scope } from '../../scope/scope'; +import { TypeChecker } from '../../typeChecker/typeChecker'; +import { GetMockPropertiesFromSymbol } from '../mock/mockProperties'; + +export function GetTypeLiteralDescriptor(node: ts.TypeLiteralNode, scope: Scope): ts.Expression { + const typeChecker: ts.TypeChecker = TypeChecker(); + const type: ts.Type = typeChecker.getTypeAtLocation(node); + + const properties: ts.Symbol[] = typeChecker.getPropertiesOfType(type); + const signatures: ReadonlyArray = type.getCallSignatures(); + + return GetMockPropertiesFromSymbol(properties, signatures, scope); +} diff --git a/test/transformer/descriptor/this/this.test.ts b/test/transformer/descriptor/this/this.test.ts index 883052f4c..86b175733 100644 --- a/test/transformer/descriptor/this/this.test.ts +++ b/test/transformer/descriptor/this/this.test.ts @@ -145,4 +145,20 @@ describe('This', () => { expect(properties.another2.getThis.prop).toBe(0); }); }); + + describe('for interface that contains a type literal', () => { + interface BaseInterface { + getThisBefore: this; + typeLiteral: { a: string }; + getThisAfter: this; + } + + it('should be able to reference to itself ', () => { + const properties: BaseInterface = createMock(); + expect(properties.typeLiteral.a).toBe(''); + expect(properties.getThisAfter.typeLiteral.a).toBe(''); + expect(properties.getThisBefore.typeLiteral.a).toBe(''); + expect(properties.getThisAfter.getThisBefore.getThisAfter.typeLiteral.a).toBe(''); + }); + }); });