ts-morph

Interface Declarations

Interface declarations can be retrieved from source files, namespaces, or function bodies:

const interfaces = sourceFile.getInterfaces();
const interface1 = sourceFile.getInterface("Interface1");
const firstInterfaceWith5Properties = sourceFile.getInterface(i => i.getProperties().length === 5);

Add/Insert

Add or insert interfaces to a source file, namespace, or function like declarations by calling addInterface(), addInterfaces(), insertInterface(), or insertInterfaces().

const interfaceDeclaration = sourceFile.addInterface({
    name: "InterfaceName"
});

Remove

Call .remove():

interfaceDeclaration.remove();

Extends expressions

Will return ExpressionWithTypeArguments[]:

const extendsExpressions = interfaceDeclaration.getExtends();

Add or insert extends expressions:

interfaceDeclaration.addExtends("Named");
interfaceDeclaration.addExtends(["Named", "Aged"]);
interfaceDeclaration.insertExtends(1, "Named");
interfaceDeclaration.insertExtends(2, ["Named", "Aged"]);

Remove an expression:

interfaceDeclaration.removeExtends(0); // index
interfaceDeclaration.removeExtends(interfaceDeclaration.getExtends()[0]); // node

Members

Get all the members of the interface:

const members = interfaceDeclaration.getMembers();

Construct signatures

Use:

const constructSignatures = interfaceDeclaration.getConstructSignatures();
const constructSignature = interfaceDeclaration.getConstructSignature(c => c.getParameters().length > 2);

Add/Insert

To add or insert use addConstructSignature(), addConstructSignatures(), insertConstructSignature, or insertConstructSignatures():

const constructSignature = interfaceDeclaration.addConstructSignature({ returnType: "SomeClass" });

Remove

Remove a construct signature:

constructSignature.remove();

Call signatures

Use:

const callSignatures = interfaceDeclaration.getCallSignatures();
const callSignature = interfaceDeclaration.getCallSignature(c => c.getParameters().length > 2);

Add/Insert

To add or insert use addCallSignature(), addCallSignatures(), insertCallSignature, or insertCallSignatures():

const callSignature = interfaceDeclaration.addCallSignature({ returnType: "SomeClass" });

Remove

Remove a call signature:

callSignature.remove();

Index signatures

Use:

const indexSignatures = interfaceDeclaration.getIndexSignatures();
const indexSignature = interfaceDeclaration.getIndexSignature(s => s.getKeyName() === "keyName");

Add/Insert

To add or insert use addIndexSignature(), addIndexSignatures(), insertIndexSignature, or insertIndexSignatures():

const indexSignature = interfaceDeclaration.addIndexSignature({
    keyName: "someKey", // defaults to key
    keyType: "string", // defaults to string
    returnType: "SomeClass"
});

Remove

Remove an index signature:

indexSignature.remove();

Method signatures

Use:

const methodSignatures = interfaceDeclaration.getMethods();
const myMethod = interfaceDeclaration.getMethod("myMethod");
const firstMethodWith4Params = interfaceDeclaration.getMethod(m => m.getParameters().length === 4);

Add/Insert

To add or insert use addMethod(), addMethods(), insertMethod, or insertMethods():

const methodSignature = interfaceDeclaration.insertMethod(1, { name: "newMethod", returnType: "boolean" });

Remove

Remove a method signature:

methodSignature.remove();

Properties

Use:

const properties = interfaceDeclaration.getProperties();
const myProperty = interfaceDeclaration.getProperty("myProperty");
const firstStringProperty = interfaceDeclaration.getProperty(p => p.getType().getText() === "string");

Add/Insert

To add or insert use addProperty(), addProperties(), insertProperty, or insertProperties():

const propertySignature = interfaceDeclaration.insertProperty(1, { name: "newProperty", type: "string" });

Remove

Remove a property signature:

propertySignature.remove();

Base Types

const baseTypes = interfaceDeclaration.getBaseTypes();

Base Declarations

Gets the base interface, type alias, or class declarations:

const baseDeclarations = interfaceDeclaration.getBaseDeclarations();

Implementations

Get the implementations.

const implementations = interfaceDeclaration.getImplementations();

Similar to "go to implementation" command available in IDEs.