ts-morph

Type Parameters

Type parameters can be retreived from nodes by calling getTypeParameters():

const typeParameters = classDeclaration.getTypeParameters();

Inserting/Adding

Insert or add type parameters by calling insertTypeParameter(), insertTypeParameters(), addTypeParameter(), or addTypeParameters().

For example:

const typeParameter = classDeclaration.insertTypeParameter(1, {
  name: "T",
  constraint: "string", // optional
});

Removing

Remove a type parameter by calling .remove() on it:

typeParameter.remove();

Constraint

Get the constraint type node:

const constraint = typeParameter.getConstraint(); // returns: TypeNode | undefined

Or set the constraint:

typeParameter.setConstraint("string");

Or remove it:

typeParameter.removeConstraint();

Default

Get the default type node:

const defaultNode = typeParameter.getDefault(); // returns: TypeNode | undefined

Or set the default type node:

typeParameter.setDefault("string");

Or remove it:

typeParameter.removeDefault();