Ohm v16October 4, 2021
Improved TypeScript support
Out of the box, Ohm mostly “just works” with TypeScript, and has for a while now. What’s new in Ohm v16 is that you can now generate type definitions for your grammars, which makes it much easier to work with semantic actions.
With grammar-specific types, the TypeScript compiler can check that your actions have the correct number of arguments and a consistent return type:
Also, IDEs such as VS Code can autocomplete action names, show tooltips with argument types (IterationNode, NonterminalNode, or TerminalNode), etc.:
How it works
To enable grammar-specific type definitions:
- Install the
npm install -D @ohm-js/clito add it as a dev dependency).
- Put your grammar in a separate .ohm file, if it isn’t already.
- Use the Ohm CLI to generate a bundle (.ohm-bundle.js) for your grammar, along with the associated type definitions (.d.ts).
For example, if your grammar is in
npx ohm generateBundles --withTypes src
src/my-grammar.ohm-bundle.d.ts. You can directly import the bundle like this:
import grammar from './my-grammar.ohm-bundle'
Then, when you create a semantics and use methods like
addOperation, you’ll see that — based on the rules in your grammar — TypeScript knows what action names can appear in the action dictionary, and what the arguments to those actions are.
For more information, see the Ohm TypeScript example.
In order to make these TypeScript improvements, we also had to make some minor breaking changes to the Ohm API — see the changelog for all the details. If you need to upgrade existing code to support Ohm v16, take a look at the upgrade guide.
If you have any questions about the new release, or need help upgrading your code to support Ohm v16, please check out the following resources:
- For issues related to the v16 release, see the Ohm v16.0.0 topic on GitHub discussions.
- For community support and discussion, join us on Discord, GitHub Discussions, or the ohm-discuss mailing list.