Skip to content
/ check Public

Type checking static analysis for Go templates.

License

Notifications You must be signed in to change notification settings

typelate/check

Repository files navigation

Check Go Reference

Check is a Go library for statically type-checking text/template and html/template. It catches template/type mismatches early, making refactoring safer when changing types or templates.

check-templates CLI

If all your ExecuteTemplate calls use a string literal for the template name and a static type for the data argument, you can use the CLI directly:

go get -tool github.com/typelate/check/cmd/check-templates
go tool check-templates ./...

Flags:

  • -v — list each call with position, template name, and data type
  • -C dir — change working directory before loading packages
  • -o format — output format: tsv (default) or jsonl

Library usage

Call Execute with a types.Type for the template's data (.) and the template's parse.Tree. See example_test.go for a working example.

Related projects

  • muxt — builds on this library to type-check templates wired to HTTP handlers. If you only need command-line checks, muxt check works too.
  • jba/templatecheck — a more mature alternative for template type-checking.

Limitations

  1. You must provide a types.Type for the template's root context (.).
  2. No support for third-party template packages (e.g. safehtml).
  3. Cannot detect runtime conditions such as out-of-range indexes or errors from boxed types.

About

Type checking static analysis for Go templates.

Topics

Resources

License

Stars

Watchers

Forks

Languages