TypeScript and TSX grammars for tree-sitter.
Because TSX and TypeScript are actually two different dialects, this module defines two grammars. Require them as follows:
require("tree-sitter-typescript").typescript; // TypeScript grammar
require("tree-sitter-typescript").tsx; // TSX grammar
For Javascript files with flow type annotations you can use the tsx
parser.
The tree-sitter
library is required for this package. Ensure you have pip
installed.
pip install tree-sitter
First, install the package using pip:
pip install .
This package allows you to load both TypeScript and TSX grammars as a Language object in Python. Here's how you can do it:
import tree_sitter_typescript as tstypescript
from tree_sitter import Language, Parser
# Load TypeScript grammar
TYPESCRIPT_LANGUAGE = Language(tstypescript.language_typescript())
# Load TSX grammar
TSX_LANGUAGE = Language(tstypescript.language_tsx())
For a practical example of how to use these grammars with tree-sitter
python library, please refer to the test file located at bindings/python/tree_sitter_typescript/test.py
.
You can test a successful installation by running the following command:
python bindings/python/tree_sitter_typescript/test.py
This will execute the test script, which demonstrates how to parse TypeScript and TSX code using the tree-sitter library.