{"_id":"@clmmatteo/type-safe-template-literal","name":"@clmmatteo/type-safe-template-literal","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@clmmatteo/type-safe-template-literal","version":"1.0.0","description":"A simple tagged template that allows to use stricter, type-safe template literals in TypeScript.","main":"dist/cjs/index.js","module":"dist/mjs/index.js","exports":{"import":"./dist/mjs/index.js","require":"./dist/cjs/index.js"},"types":"dist/types/index.d.ts","scripts":{"build":"rm -fr dist/* && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && sh ./fixup","prepare":"npm run build"},"repository":{"type":"git","url":"git+https://github.com/clmmatteo/type-safe-template-literal.git"},"type":"module","keywords":["typescript","template literals","tagged templates","typesafe","type-safe"],"author":{"name":"Matteo Colombo"},"license":"MIT","bugs":{"url":"https://github.com/clmmatteo/type-safe-template-literal/issues"},"homepage":"https://github.com/clmmatteo/type-safe-template-literal#readme","devDependencies":{"@types/node":"^18.6.3","typescript":"^4.7.4"},"gitHead":"98ccb991f54620c3a5593c88724ed8fb94261cfa","_id":"@clmmatteo/type-safe-template-literal@1.0.0","_nodeVersion":"16.16.0","_npmVersion":"8.11.0","dist":{"integrity":"sha512-aYxfXZw2GH3rapolbgjaFRWqCxhiALcAi0rWCGU4IT+ceLtyW7ZA+E5YKu23Z8vEVnb3SjcqWZ58FRu6YYzhhw==","shasum":"719446b5fef8549f5ecf48e31481f7cb53e38404","tarball":"https://registry.npmjs.org/@clmmatteo/type-safe-template-literal/-/type-safe-template-literal-1.0.0.tgz","fileCount":13,"unpackedSize":8336,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIAYYUXN2foGPDABB8K5fMOOcbzMUl5DQFv/Px4IAnDMQAiEAjKmP2Qf9mmb9rYYxveJPI/gFkPr5xn/aCgnejSCCidc="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJi66PKACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmqr4BAAnGMTodFvDCYxC63ykbOuguPgp29x3StPVsbjvwGc83ReOuSJ\r\n+y1rVbcVlpY9AsEpVfzgOe9JDTzSN9dyBZnGpxvC/H4JMahCbHQAO74p45Vo\r\nGgjWX6gLO2Feyvwj5oAeaPedCq1Z+1ECKCpehWN6dZ4llu+AF9SSfpPkYyHJ\r\nFXCzm5Dy7ZOs2t7TdN5T9TZ7oCtSlcN3qhQ8GRMbq15+CO7t7EUdOo1wlHD7\r\ntzOnrpgKIXRrIwMtVH763WYkf/B4jDmT4R995NolRoRsahr2wghFFU4rAVFn\r\nfLrdkmu9mWXLuIde1UMjyVOGlWs4pUZB0wE11w3UbUPYzjNFIap9B7oe362v\r\nuQnMGwhOSHvMM+hjLMNcPoN31wUOzerO664giZZ9sAg6y3kDhS/QgRyqKEHg\r\nbZ/VtiE/eh70KG4ZcYtjX+Y/lmmUN/3QPRpHkq8f8ZMxVceEOmjRrNcbRrPS\r\nHbBaYjCYETpBu1ExMCqvhyf0ImBqu1fBpy7dbjEUE+eZ7ybMFpiDd3K57bxN\r\n+kANaWt0c5VAZ75oLJ0GcYsSl4lcnlyKncyMLRyCB9oas9cXTTLTggV1v49Y\r\nEUVuFNl+/P6dRH/8M32FSx5k61+SmOnQnzxvHlfoQF4dTpjflocu2to5RAvV\r\n14rgcb2NlYj0/G4/cm6Zx18GFC8jFgpEOQc=\r\n=Sh8N\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"clmmatteo","email":"open@clmmatteo.com"},"directories":{},"maintainers":[{"name":"clmmatteo","email":"open@clmmatteo.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/type-safe-template-literal_1.0.0_1659610058765_0.4073812288931107"},"_hasShrinkwrap":false}},"time":{"created":"2022-08-04T10:47:38.697Z","1.0.0":"2022-08-04T10:47:38.902Z","modified":"2022-08-04T10:47:39.038Z"},"maintainers":[{"name":"clmmatteo","email":"open@clmmatteo.com"}],"description":"A simple tagged template that allows to use stricter, type-safe template literals in TypeScript.","homepage":"https://github.com/clmmatteo/type-safe-template-literal#readme","keywords":["typescript","template literals","tagged templates","typesafe","type-safe"],"repository":{"type":"git","url":"git+https://github.com/clmmatteo/type-safe-template-literal.git"},"author":{"name":"Matteo Colombo"},"bugs":{"url":"https://github.com/clmmatteo/type-safe-template-literal/issues"},"license":"MIT","readme":"# type-safe-template-literal\n\nA simple tagged template that allows to use stricter, type-safe template literals in TypeScript.\n\n## Installation\n\nThe package can be used both with CommonJS `require` and ESM `import`. For readability purposes, it is recommended to alias the function with a simpler word (in the following examples, the function is assigned to \"t\").\n\n```sh\nnpm install @clmmatteo/type-safe-template-literal\n```\n\n```ts\n//If you use CommonJS\nconst {\n  typeSafeTemplateLiteral: t,\n} = require(\"@clmmatteo/type-safe-template-literal\");\n```\n\n```ts\n//If you use ESM modules\nimport { typeSafeTemplateLiteral as t } from \"@clmmatteo/type-safe-template-literal\";\n```\n\n## The problem\n\nIn TypeScript, this is perfectly valid code:\n\n```ts\nfunction greeting(name?: string): string {\n  return `Hi! My name is ${name}!`;\n}\nconsole.log(greeting());\n//Result: \"Hi! My name is undefined!\"\n```\n\nIn other words, any type that can be coerced to a string (including `undefined`, `null`, and objects that have a `.toString()` method) can be used in a template literal without any warning.\n\n## The solution\n\nIf you want stricter warnings, you can use the tagged template exported by this package in order to **only allow strings** inside your template literal.\n\nUsing the previous example (notice the addition of the tagged template _t\\`...\\`_ in the return expression):\n\n```ts\nfunction greeting(name?: string): string {\n  return t`Hi! My name is ${name}!`;\n  //                        ^^^^\n  // Type 'undefined' is not assignable to type 'string'.\n}\n```\n\nTypeScript warns us when using variables that aren't, or may not be, strings, so that we can make informed decisions.\n\n```ts\nfunction greeting(name?: string): string {\n  return t`Hi! My name is ${name ? name : \"still to be decided\"}!`;\n}\nconsole.log(greeting());\n//Result: \"Hi! My name is still to be decided!\"\n```\n\n## Other effects\n\nAny variable that isn't, or may not be, a string, triggers an error (even numbers!). This forces you to better think about how you want to format the string representation of the data you're using.\n\nIn the following example, the function is using numbers instead of strings. How should very big numbers, or numbers with decimals, be formatted?\n\n```ts\nfunction multiplier(x: number, y: number): string {\n  return t`${x} times ${y} is ${x * y}`;\n  //         ^          ^       ^^^^^\n  //Argument of type 'number' is not assignable to parameter of type 'string'.\n}\n```\n\nIn the following example, the function is using a date. How should the date be formatted?\n\n```ts\nfunction sayToday(): string {\n  return t`Today is ${new Date()}`;\n  //                  ^^^^^^^^^^\n  //Argument of type 'Date' is not assignable to parameter of type 'string'.\n}\n```\n\nIn the following example, the `greeting` method of the class is using a property that may be undefined.\n\n```ts\nclass Human {\n  name?: string;\n\n  constructor(name?: string) {\n    this.name = name;\n  }\n\n  greeting(): string {\n    return t`Hi! My name is ${this.name}!`;\n    //                        ^^^^^^^^^\n    //Type 'undefined' is not assignable to type 'string'.\n  }\n}\n```\n","readmeFilename":"README.md"}