{"id":26258772,"url":"https://github.com/mattstrom/typesafe-templates","last_synced_at":"2025-10-27T21:11:22.970Z","repository":{"id":33193541,"uuid":"154646984","full_name":"mattstrom/typesafe-templates","owner":"mattstrom","description":"Template engine that leverages JSX to generate JavaScript code from TypeScript code files rather than text templates.","archived":false,"fork":false,"pushed_at":"2025-04-01T12:11:32.000Z","size":1366,"stargazers_count":34,"open_issues_count":20,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-09T23:14:52.912Z","etag":null,"topics":["javascript","jsx","template-engine","tsx","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mattstrom.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-25T09:46:27.000Z","updated_at":"2025-03-15T14:02:07.000Z","dependencies_parsed_at":"2024-05-02T06:36:57.839Z","dependency_job_id":"c60a336f-68f8-43c2-9cc6-be0e6a5a2f22","html_url":"https://github.com/mattstrom/typesafe-templates","commit_stats":{"total_commits":103,"total_committers":7,"mean_commits":"14.714285714285714","dds":0.4368932038834952,"last_synced_commit":"b1aacef6800a9cb7d2f6875a54eae4196464c423"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattstrom%2Ftypesafe-templates","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattstrom%2Ftypesafe-templates/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattstrom%2Ftypesafe-templates/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattstrom%2Ftypesafe-templates/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattstrom","download_url":"https://codeload.github.com/mattstrom/typesafe-templates/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248125591,"owners_count":21051770,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","jsx","template-engine","tsx","typescript"],"created_at":"2025-03-13T22:14:07.343Z","updated_at":"2025-10-27T21:11:17.939Z","avatar_url":"https://github.com/mattstrom.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"typesafe-templates\n====================\n[![CircleCI](https://circleci.com/gh/mattstrom/typesafe-templates.svg?style=svg)](https://circleci.com/gh/mattstrom/typesafe-templates)\n[![npm version](https://img.shields.io/npm/v/typesafe-templates/latest.svg)](https://www.npmjs.com/package/typesafe-templates)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n\nTemplate engine for writing compiler-checked templates in TypeScript by leveraging JSX to generate JavaScript code from \nTypeScript code files rather than text templates.\n\nUnder the hood, `typesafe-templates` uses Babel to build an AST, which is then traversed to find and replace JSX nodes.\nSuch a tool can be useful for pre-generating customized Javascript files that vary by user or for creating HTML templates using\nJSX rather than a syntax like Pug (some limitations apply). \n\n### Example\n_template.tsx_\n```\ninterface Message {\n    name: string;\n    lang: 'en' | 'es';\n}\n\n(function() {\n    \u003c$repeat items={$.messages}\u003e\n        {($: Message) =\u003e {\n            \u003c$if test={$.lang === 'en'}\u003e\n                console.log('Good morning' + \u003c$string value={$.name} /\u003e);\n            \u003c/$if\u003e;\n        \t\u003c$if test={$.lang === 'es'}\u003e\n                console.log('Bueños dias' + \u003c$string value={$.name} /\u003e);\n            \u003c/$if\u003e;\n        }}\n    \u003c/$repeat\u003e\n})();\n```\n\n_script.ts_\n```\nimport { renderFile } from 'typesafe-templates';\n\nasync function main() {\n    const data: { messages: Message[] } = {\n        messages: [\n            { name: 'Alice', lang: 'en' },\n            { name: 'Bob', lang: 'en' },\n            { name: 'Dora', lang: 'es' },\n            { name: 'Diego', lang: 'es' }\n        ]\n    };\n    \n    const { code } = await renderFile('./template.tsx', data);\n    console.log(code);\n}\n\nmain();\n```\n\nOutput\n```\n(function() {\n    console.log('Good morning, Alice');\n    console.log('Good morning, Bob');\n    console.log('Bueños dias, Dora');\n    console.log('Bueños dias, Diego');\n})();\n```\n\n### Installation\n\n```\nnpm install typesafe-templates\n```\n\n\u003e Use the `--save-dev` flag if you will be generating templates as part of a development task.\n\n### Usage\nTo work properly you will need to include a type definition for JSX elements that assigns `JSX.Element`\nto `any`.\n\n```\ndeclare namespace JSX {\n    type Element = any;\n}\n```\n\nAs a convenience, the `$` symbol is used to represent a parent type of a scope. However,\nany name can be used. Presently the library does not support deep properties. A reference like\n`$.level1.level2.level3` may type-check correctly, but the proper value will not be injected into\nthe template.\n\n### Elements\n\n#### Control Elements\nControl elements are similar to JavaScript control blocks. They wrap a block of code and control\nits output into the rendered code. In JSX terms, they require `props.children` to be defined.\n\nBecause JSX is used here as substitute JavaScript expressions, you will often need to include\na semicolon after the tag as you would with a normal statement.\n##### `\u003c$if test={} /\u003e`\nControls whether the wrapped contents will appear in the output.\n\n##### `\u003c$repeat items={} /\u003e`\nRepeats the wrapped contents for each item in `items`. For each copy, a new data scope is created\nand set to value of the current item.\n\nThe `$repeat` element expects the `children` prop to be a function or arrow function expression; however, this\nsurrounding function will be removed in the final output.\n\nExample:\n```\n\u003c$repeat items={$.messages}\u003e\n    {($: Message) =\u003e {\n    \tconsole.log(\u003c$string value={$.name}\u003e)\n    }}\n\u003c/$repeat\u003e\n```\n\n#### Injection Elements\n##### `\u003c$boolean value={} /\u003e`\nOutputs a boolean literal.\n\n##### `\u003c$expr code={} /\u003e`\nTakes in a string representing an expression, parses it into AST, and adds the resultant node into the output.\n\n##### `\u003c$number value={} /\u003e`\nOutputs a numeric literal.\n\n##### `\u003c$string value={} /\u003e`\nOutputs a string literal.\n\n### Limitations\nCurrently TypeScript treats all JSX elements as the same type (which can be changed but only to one collective type). Therefore\nelements, when used as values, are treated as `any` and will not show type errors unless you manually\ntypecast the element. Refactoring signatures, however, will work when using TS tooling to rearrange arguments. \n\nExample:\n```\nfunction print(str: string, num: number, bool: boolean) {}\n\nprint(\u003c$string /\u003e, \u003c$string /\u003e, \u003c$string /\u003e); // Will not report type errors\n\nprint(\u003c$string /\u003e as string, \u003c$string /\u003e as string, \u003c$string /\u003e as string); // Will report type errors\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattstrom%2Ftypesafe-templates","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattstrom%2Ftypesafe-templates","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattstrom%2Ftypesafe-templates/lists"}