ts-morph
Parameters
Parameters can be retreived from nodes by calling getParameters()
:
const parameters = functionDeclaration.getParameters();
Inserting/Adding
Insert or add parameters by calling insertParameter()
, insertParameters()
, addParameter()
, or addParameters()
.
For example:
const parameter = functionDeclaration.insertParameter(1, {
name: "param1",
type: "string",
});
Removing
Remove a parameter by calling .remove()
on it:
parameter.remove();
Rest parameter
Tell if a parameter is a rest parameter:
const isRestParameter = parameter.isRestParameter(); // returns: boolean
Or set a parameter as a rest parameter:
parameter.setIsRestParameter(true);
Parameter Property
Constructor parameters may be properties when they have a scope and/or are readonly. You can tell if one is by calling:
const isParameterProperty = parameter.isParameterProperty(); // returns: boolean
Optional
A parameter can be optional if it is marked so with a question mark, is a rest parameter, or has an initializer.
isOptional()
can be used to tell if any of these are true:
const isOptional = parameter.isOptional(); // returns: boolean