{"id":2817,"library":"tree-sitter-typescript","title":"tree-sitter-typescript","description":"tree-sitter-typescript provides pre-compiled TypeScript and TSX grammars for the `tree-sitter` parsing library in Python. It allows developers to parse TypeScript and TSX code into concrete syntax trees for advanced code analysis and manipulation. The library is actively and regularly updated, with the current stable version being 0.23.2.","status":"active","version":"0.23.2","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter/tree-sitter-typescript","tags":["tree-sitter","parsing","typescript","tsx","grammar","ast","code analysis"],"install":[{"cmd":"pip install tree-sitter tree-sitter-typescript","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Provides the core parsing engine and Python bindings required to load and use the grammars.","package":"tree-sitter","optional":false}],"imports":[{"note":"Used to load the standard TypeScript grammar.","symbol":"language","correct":"from tree_sitter import Language, Parser\nimport tree_sitter_typescript\nTS_LANGUAGE = Language(tree_sitter_typescript.language(), 'typescript')"},{"note":"Used to load the TSX (TypeScript React) grammar.","symbol":"tsx_language","correct":"from tree_sitter import Language, Parser\nimport tree_sitter_typescript\nTSX_LANGUAGE = Language(tree_sitter_typescript.tsx_language(), 'tsx')"}],"quickstart":{"code":"from tree_sitter import Language, Parser\nimport tree_sitter_typescript\n\n# Load the TypeScript grammar\nTS_LANGUAGE = Language(tree_sitter_typescript.language(), 'typescript')\n\n# Create a parser and set its language\nparser = Parser()\nparser.set_language(TS_LANGUAGE)\n\n# Code to parse\nts_code = b\"\"\"interface MyInterface { name: string; age?: number; }\\nconst x: MyInterface = { name: 'Alice' };\"\"\"\n\n# Parse the code\ntree = parser.parse(ts_code)\n\n# Access the root node\nroot_node = tree.root_node\nprint(f\"Parsed TypeScript root node type: {root_node.type}\")\n\n# Load the TSX grammar\nTSX_LANGUAGE = Language(tree_sitter_typescript.tsx_language(), 'tsx')\nparser.set_language(TSX_LANGUAGE)\n\n# TSX code to parse\ntsx_code = b\"\"\"function App() { return <div>Hello, {name}</div>; }\"\"\"\n\n# Parse the TSX code\ntree_tsx = parser.parse(tsx_code)\nroot_node_tsx = tree_tsx.root_node\nprint(f\"Parsed TSX root node type: {root_node_tsx.type}\")\n\n# Example of finding a node\ninterface_node = root_node.children[0] # Assuming interface declaration is the first child\nprint(f\"First node in TS: {interface_node.type}, text: {interface_node.text.decode('utf8')}\")","lang":"python","description":"This quickstart demonstrates how to install `tree-sitter-typescript` and `tree-sitter`, then load both the TypeScript and TSX grammars to parse respective code snippets. It shows how to initialize a parser, set the language, parse code, and access the resulting syntax tree's root node."},"warnings":[{"fix":"Ensure you are using a recent version of `tree-sitter` (the core Python binding) and directly `pip install tree-sitter-typescript`. Do not attempt to use `Language.build_library`.","message":"The `Language.build_library` method, previously used to compile grammars in older versions of `py-tree-sitter` (prior to approximately 0.22.x), has been removed. Grammars like `tree-sitter-typescript` are now expected to be installed as pre-compiled Python packages.","severity":"breaking","affected_versions":"<=0.21.x of `py-tree-sitter`"},{"fix":"Always ensure your `tree-sitter` Python package and `tree-sitter-typescript` are kept up-to-date and compatible. If parsing issues arise after upgrading `tree-sitter`, update `tree-sitter-typescript` as well.","message":"The underlying `tree-sitter` core library occasionally introduces Application Binary Interface (ABI) changes (e.g., ABI 15 in v0.25.0). These changes can cause compatibility issues with older `tree-sitter` Python bindings or grammar packages if they are not updated to match the new ABI.","severity":"breaking","affected_versions":"All versions of `tree-sitter-typescript` when paired with incompatible `tree-sitter` core versions"},{"fix":"Monitor GitHub issues for known parsing limitations. If encountering `ERROR` nodes for valid syntax, consider reporting it as a bug to the `tree-sitter-typescript` GitHub repository.","message":"While `tree-sitter-typescript` provides both TypeScript and TSX grammars, complex or less common syntax constructs (e.g., specific styled components patterns, LaTeX strings within templates, or certain advanced `import`/`export` declarations) may result in `ERROR` or `MISSING` nodes in the parsed syntax tree. This indicates the grammar could not fully parse that section.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If working with mixed JavaScript/TypeScript files or encountering issues that seem related to underlying JavaScript parsing, explicitly `pip install tree-sitter-javascript` alongside `tree-sitter-typescript`.","message":"The `tree-sitter-typescript` grammar internally relies on the `tree-sitter-javascript` grammar for JavaScript-specific constructs. However, installing `tree-sitter-typescript` via pip does not automatically install `tree-sitter-javascript` as a separate Python package. This might lead to unexpected parsing behavior or errors if the JavaScript grammar is implicitly expected for certain parts of the code.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}