Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tree-sitter/tree-sitter/llms.txt
Use this file to discover all available pages before exploring further.
Introduction
In languages with static typing, it can be helpful for syntax trees to provide specific type information about individual syntax nodes. Tree-sitter makes this information available via a generated file callednode-types.json.
This node types file provides structured data about every possible syntax node in a grammar. You can use this data to generate type declarations in statically-typed programming languages.
File Location
Thenode-types.json file is generated when you build a grammar and is typically located in the src/ directory of a parser:
File Structure
The node types file contains an array of objects, each describing a particular type of syntax node.Basic Information
Every object in the array has these two entries:Indicates which grammar rule the node represents. This corresponds to the
ts_node_type function.Indicates whether this node corresponds to a rule name in the grammar (
true) or just a string literal (false).Examples
Named Node
Anonymous Node
Together, the
type and named fields constitute a unique identifier for a node type. No two top-level objects in node-types.json should have the same values for both fields.Internal Nodes
Many syntax nodes can have children. The node type object describes possible children using these entries:An object describing the possible fields that the node can have. Keys are field names, and values are child type objects.
A child type object describing all the node’s possible named children without fields.
Child Type Objects
A child type object describes a set of child nodes:Whether there is always at least one node in this set
Whether there can be multiple nodes in this set
An array of objects representing the possible types of nodes in this set. Each object has
type and named keys.Example with Fields
method_definition node that:
- Has a required
bodyfield (always astatement_block) - May have zero or more
decoratorfields - Has a required
namefield (eithercomputed_property_nameorproperty_identifier) - Has a required
parametersfield (alwaysformal_parameters)
Example with Children
array node that can contain zero or more children, where each child is either an _expression or a spread_element.
Supertype Nodes
In Tree-sitter grammars, certain rules represent abstract categories of syntax nodes (e.g., “expression”, “type”, “declaration”). In thegrammar.js file, these are often written as hidden rules whose definition is a simple choice where each member is just a single symbol.
Normally, hidden rules don’t appear in the node types file. But if you add a hidden rule to the grammar’s supertypes list, then it will show up in the node types file with a special entry:
An array of objects that specify the types of nodes this ‘supertype’ node can wrap.
Example
Using Node Types for Code Generation
Thenode-types.json file can be used to generate type-safe bindings for your language. Here’s an example of how you might process it:
Real-World Example
Here’s how the TypeScript parser for tree-sitter-typescript uses node types to generate TypeScript definitions:Benefits of Using Node Types
Type Safety
Generate type-safe code that prevents runtime errors when working with syntax trees.
IDE Support
Enable autocomplete and inline documentation in your IDE.
Refactoring
Safely refactor code that works with syntax trees.
Documentation
Auto-generate documentation about your grammar’s structure.
Accessing Node Types at Runtime
You can also access node type information programmatically:Next Steps
ABI Versions
Learn about parser compatibility and ABI versioning
Creating Parsers
Build your own Tree-sitter parser