### Install ts-morph library Source: https://ts-morph.com/details/index/index Instructions for installing the ts-morph library using different package managers and runtimes. ```shell npm install --save-dev ts-morph ``` ```shell deno add ts-morph@jsr:@ts-morph/ts-morph ``` -------------------------------- ### Output of ts-morph getExportedDeclarations example Source: https://ts-morph.com/details/index/details/exports Shows the expected console output when running the `getExportedDeclarations()` example, illustrating how different types of exports (classes, aliased interfaces, merged namespaces, default exports) are represented. ```Plaintext Class1: export class Class1 {} Class2: export class Class2 {} AliasedInterface: export interface Interface1 {} MergedNamespace: namespace MergedNamespace { let t; }, namespace MergedNamespace { let u; } default: 5 ``` -------------------------------- ### TypeScript Literal Syntax Examples Source: https://ts-morph.com/details/index/details/literals Examples of different literal types as they appear in TypeScript code, which ts-morph can parse and represent in its Abstract Syntax Tree (AST). ```TypeScript "some string" ``` ```TypeScript 5 ``` ```TypeScript 10.53 ``` ```TypeScript true ``` ```TypeScript false ``` ```TypeScript `some string` ``` ```TypeScript /pattern/gi ``` -------------------------------- ### Example TypeScript files for exported declarations Source: https://ts-morph.com/details/index/details/exports Provides example TypeScript file content (`main.ts`, `classes.ts`, `Class1.ts`, `Class2.ts`, `interfaces.ts`) to demonstrate various export scenarios, including re-exports, aliased exports, merged namespaces, and default exports. ```TypeScript // main.ts export * from "./classes"; export { Interface1 as AliasedInterface } from "./interfaces"; namespace MergedNamespace { let t; } namespace MergedNamespace { let u; } export { MergedNamespace }; export default 5; // classes.ts export * from "./Class1"; export * from "./Class2"; // Class1.ts export class Class1 {} // Class2.ts export class Class2 {} // interfaces.ts export interface Interface1 {} export interface Interface2 {} ``` -------------------------------- ### Instantiating a Basic ts-morph Project Source: https://ts-morph.com/details/index/setup/index Demonstrates the fundamental way to create a new Project instance from the 'ts-morph' library without any specific configurations. ```TypeScript import { Project } from "ts-morph"; const project = new Project(); ``` -------------------------------- ### Get Position and Length of a Diagnostic in ts-morph Source: https://ts-morph.com/details/index/setup/diagnostics Demonstrates how to retrieve the starting position, line number, and length of a diagnostic within its source file, useful for pinpointing error locations. ```TypeScript const start = diagnostic.getStart(); // returns: number const lineNumber = diagnostic.getLineNumber(); // returns: number const length = diagnostic.getLength(); // returns: number ``` -------------------------------- ### Instantiating ts-morph Project from tsconfig.json Source: https://ts-morph.com/details/index/setup/index Illustrates how to initialize a Project by specifying the path to an existing tsconfig.json file, which automatically adds all associated source files. ```TypeScript const project = new Project({ tsConfigFilePath: "path/to/tsconfig.json", }); ``` -------------------------------- ### Retrieving Apparent Type in ts-morph Source: https://ts-morph.com/details/index/details/types Explains how to get the apparent type of a variable, which might differ from its literal type, using the `getApparentType()` method. For example, a literal `4` has an apparent type of `Number`. ```TypeScript const myVar = 4; ``` ```TypeScript const apparentType = type.getApparentType(); ``` -------------------------------- ### Instantiating a basic ts-morph Project Source: https://ts-morph.com/details/index/setup Demonstrates the simplest way to create a new Project instance using the `Project` class from 'ts-morph'. ```TypeScript import { Project } from "ts-morph"; const project = new Project(); ``` -------------------------------- ### Getting Initializers from AST Nodes in ts-morph Source: https://ts-morph.com/details/index/details/initializers Demonstrates how to retrieve initializer expressions from variable declarations using various `getInitializer` methods provided by ts-morph. Includes an example of a function expression initializer and the corresponding API calls. ```javascript const add = function(a: number, b: number) { return a + b; }; ``` ```typescript variableDeclaration.getInitializer(); // returns: Expression | undefined variableDeclaration.getInitializerOrThrow(); // returns: Expression variableDeclaration.getInitializerIfKind(SyntaxKind.FunctionExpression); // returns: Expression | undefined variableDeclaration.getInitializerIfKindOrThrow(SyntaxKind.FunctionExpression); // returns: Expression ``` -------------------------------- ### Get Structure of a Node in ts-morph Source: https://ts-morph.com/details/index/manipulation/structures Demonstrates how to retrieve the simplified AST representation (structure) of a node using `node.getStructure()`. This method works for various node types like classes, interfaces, and enums. It also shows an example of the returned structure object. ```TypeScript // example with a class declaration, but this also works on interfaces, enums, and many other nodes. const classStructure = classDeclaration.getStructure(); // returns: ClassDeclarationStructure ``` ```TypeScript { isAbstract: false, isExported: true, name: "MyClass", typeParameters: [], constructors: [], properties: [{ name: "myProp", initializer: "5", type: undefined, isReadonly: false, isStatic: false }], methods: [] } ``` -------------------------------- ### Retrieve emit output details for a source file Source: https://ts-morph.com/details/index/emitting This example demonstrates how to get the emit output object for a source file, allowing inspection of whether the emit was skipped, and iterating through the generated output files to access their paths, byte order marks, and text content. ```TypeScript const emitOutput = sourceFile.getEmitOutput(); emitOutput.getEmitSkipped(); // returns: boolean for (const outputFile of emitOutput.getOutputFiles()) { outputFile.getFilePath(); outputFile.getWriteByteOrderMark(); outputFile.getText(); } ``` -------------------------------- ### Instantiating ts-morph Project with Compiler Options Source: https://ts-morph.com/details/index/setup Shows how to initialize a Project instance and configure TypeScript compiler options, such as the target ECMAScript version. ```TypeScript import { Project, ScriptTarget } from "ts-morph"; const project = new Project({ compilerOptions: { target: ScriptTarget.ES3, }, }); ``` -------------------------------- ### Instantiating ts-morph Project with Compiler Options Source: https://ts-morph.com/details/index/setup/index Shows how to create a Project instance and configure TypeScript compiler options directly, such as setting the target ECMAScript version. ```TypeScript import { Project, ScriptTarget } from "ts-morph"; const project = new Project({ compilerOptions: { target: ScriptTarget.ES3, }, }); ``` -------------------------------- ### Get Corresponding Get Accessor for a Set Accessor in ts-morph Source: https://ts-morph.com/details/index/details/classes Retrieves the corresponding get accessor for a given set accessor using `getGetAccessor()`, if one exists. ```typescript const getAccessor = setAccessor.getGetAccessor(); ``` -------------------------------- ### Instantiating ts-morph Project from tsconfig.json Source: https://ts-morph.com/details/index/setup Explains how to create a Project instance by specifying the path to a tsconfig.json file, which automatically adds associated source files. ```TypeScript const project = new Project({ tsConfigFilePath: "path/to/tsconfig.json", }); ``` -------------------------------- ### Get Corresponding Set Accessor for a Get Accessor in ts-morph Source: https://ts-morph.com/details/index/details/classes Retrieves the corresponding set accessor for a given get accessor using `getSetAccessor()`, if one exists. ```typescript const setAccessor = getAccessor.getSetAccessor(); ``` -------------------------------- ### Initialize ts-morph Project with Partial Manipulation Settings Source: https://ts-morph.com/details/index/manipulation/settings Shows how to create a `ts-morph` Project instance by providing only a subset of the manipulation settings, allowing for specific overrides without defining all defaults. ```typescript const project = new Project({ manipulationSettings: { indentationText: IndentationText.TwoSpaces }, }); ``` -------------------------------- ### Getting Definition Nodes of an Identifier in ts-morph Source: https://ts-morph.com/details/index/details/identifiers Illustrates how to directly get the definition nodes associated with an identifier, providing access to the underlying AST nodes. ```TypeScript const nodes = identifier.getDefinitionNodes(); ``` -------------------------------- ### Implementing a Custom File System Host in ts-morph Source: https://ts-morph.com/details/index/setup/file-system This example illustrates how to provide a custom file system implementation to a ts-morph Project. Users can create a class that implements the `FileSystemHost` interface and then pass an instance of it during project initialization, allowing for highly customized file operations and integration with non-standard file systems. ```TypeScript import { FileSystemHost, Project } from "ts-morph"; class MyCustomFileSystem implements FileSystemHost { // implement it } const fs = new MyCustomFileSystem(); const project = new Project({ fileSystem: fs }); ``` -------------------------------- ### Get Parent Export Declaration of Named Export in ts-morph Source: https://ts-morph.com/details/index/details/exports Shows how to get the parent `ExportDeclaration` object from an individual `NamedExport` (export specifier). ```APIDOC NamedExport (Export Specifier) Parent: getExportDeclaration(): ExportDeclaration - Returns the parent export declaration. ``` ```TypeScript namedExport.getExportDeclaration(); // returns: ExportDeclaration ``` -------------------------------- ### Initialize ts-morph Project and Add Source Files Source: https://ts-morph.com/details/index/navigation/example This code initializes a new ts-morph project instance. It then adds all TypeScript files found in the current directory and its subdirectories to the project, making them available for parsing and manipulation. ```TypeScript import { Project } from "ts-morph"; const project = new Project(); project.addSourceFilesAtPaths("**/*.ts"); ``` -------------------------------- ### Instantiating ts-morph Project and Skipping tsconfig.json File Addition Source: https://ts-morph.com/details/index/setup/index Explains how to create a Project instance using a tsconfig.json file while preventing ts-morph from automatically adding the associated source files. ```TypeScript const project = new Project({ tsConfigFilePath: "path/to/tsconfig.json", skipAddingFilesFromTsConfig: true, }); ``` -------------------------------- ### Get Source File of a Diagnostic in ts-morph Source: https://ts-morph.com/details/index/setup/diagnostics Explains how to get the `SourceFile` object where a specific diagnostic occurs. This method returns `undefined` if the source file is not available. ```TypeScript const sourceFile = diagnostic.getSourceFile(); // returns: SourceFile | undefined ``` -------------------------------- ### Add a Get Accessor to a Class in ts-morph Source: https://ts-morph.com/details/index/details/classes Demonstrates adding a get accessor to a class using `addGetAccessor()`, defining its name, return type, and statements. ```typescript const getAccessor = classDeclaration.addGetAccessor({ name: "someNumber", returnType: "number", statements: ["return 5;"], }); ``` -------------------------------- ### Configuring Custom Module Resolution in ts-morph Project Source: https://ts-morph.com/details/index/setup/index Demonstrates how to implement custom module resolution logic, such as Deno-style path handling, by providing a resolution host factory function to the Project constructor. ```TypeScript import { Project, ts } from "ts-morph"; // this is deno style module resolution (ex. `import { MyClass } from "./MyClass.ts"`) const project = new Project({ resolutionHost: (moduleResolutionHost, getCompilerOptions) => { return { resolveModuleNames: (moduleNames, containingFile) => { const compilerOptions = getCompilerOptions(); const resolvedModules: ts.ResolvedModule[] = []; for (const moduleName of moduleNames.map(removeTsExtension)) { const result = ts.resolveModuleName(moduleName, containingFile, compilerOptions, moduleResolutionHost); if (result.resolvedModule) resolvedModules.push(result.resolvedModule); } return resolvedModules; } }; function removeTsExtension(moduleName: string) { if (moduleName.slice(-3).toLowerCase() === ".ts") return moduleName.slice(0, -3); return moduleName; } } }); ``` -------------------------------- ### Specifying Custom libFolderPath for ts-morph Project Source: https://ts-morph.com/details/index/setup/index Shows how to configure the Project to load TypeScript's lib.d.ts files from a specific folder on the file system instead of using the default in-memory path. ```TypeScript const project = new Project({ libFolderPath: "./node_modules/typescript/lib", }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.