Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Can anyone explain to me the purpose of tagged template strings?

From what I understand it’s only a function that takes in a string, so this might have just been sql(‘…’).




Tags are functions that receive an array of strings and then a series of arguments corresponding to values passed in the interpolation slots, allowing you to use template literal syntax while the tag function does something much more complex under the hood, potentially including returning non-string values.


Ohhh got it, thanks!


You can use expressions inside template literals and those are auto-escaped for you by postgres.js


What do you mean? This is true for any template string, not specifically the tagged template strings, no?

Edit: I think I understand now from the other comment, thanks!


Also, template strings like sql`foobar`, graphql`foobar` and others enable syntax awareness in IDEs.

I have a big Typescript / SQL mixed codebase (No ORM), and its very nice to have VSCode syntax color and code-format TypeScript, GraphQL and SQL in the same file




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: