move_symbol_to_file_by_tsmorph
Move a top-level symbol (function, variable, class, etc.) between files, automatically rewriting imports/exports and carrying internal-only dependencies.
Instructions
[ts-morph] Move one top-level symbol (function, variable, class, interface, type, enum) from one file to another, carrying its internal-only dependencies and rewriting all imports/exports across the project.
When to use
Splitting a large file: move related symbols to a new file one by one.
Relocating a helper from a generic
utils.tsto a feature-specific module.Prefer this over manual cut-and-paste + import fixing. Manual moves frequently miss re-exports, leave stale imports, or fail to add the new export -- this tool handles all of that via the type checker.
When NOT to use
Renaming the file (without moving a single symbol out of it) ->
rename_filesystem_entry_by_tsmorph.Renaming a symbol in place ->
rename_symbol_by_tsmorph.The symbol you want to move is a
export default-> NOT SUPPORTED, refactor it to a named export first.
Critical constraints
ONE top-level symbol per call. To move N symbols, invoke the tool N times.
Default exports CANNOT be moved. Convert them to named exports beforehand.
If multiple top-level declarations share the same name (e.g., function + namespace), pass
declarationKindString(e.g.,"FunctionDeclaration","VariableStatement") to disambiguate.Internal dependency rules:
Dependencies used ONLY by the moved symbol travel with it.
Dependencies also used by other symbols in the source file stay put, gain
exportif missing, and are imported back into the destination file.
All paths (
tsconfigPath,originalFilePath,targetFilePath) MUST be absolute.targetFilePathmay point to a non-existent file; it will be created.
Tips
Run with
dryRun: truefirst when the source file has many co-dependencies to confirm what gets pulled along.
Result
Returns the list of modified (or to-be-modified, in dryRun) file paths, plus status and processing time.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| tsconfigPath | Yes | Absolute path to the project's tsconfig.json file. Essential for ts-morph. | |
| originalFilePath | Yes | Absolute path to the file containing the symbol to move. | |
| targetFilePath | Yes | Absolute path to the destination file. Can be an existing file; if the path does not exist, a new file will be created. | |
| symbolToMove | Yes | The name of the single top-level symbol you want to move in this execution. | |
| declarationKindString | No | Optional. The kind of the declaration. Providing this helps resolve ambiguity if multiple symbols share the same name. | |
| dryRun | No | If true, only show intended changes without modifying files. |