what are template literals types and what are template literals?



This content originally appeared on DEV Community and was authored by omkar garde

template literals types in typescript are built upon literal type.

a literal type represent that a variable can hold only one specific string.

a regular which can represent any string value.

let changingString = "Hello World";
changingString = "Goodbye World";
// Because `changingString` can represent any possible string, that
// is how TypeScript describes it in the type system
changingString;

const constantString = "Hello World";
// Because `constantString` can only represent 1 possible string, it
// has a literal type representation
// NOTE: literal types are still string and returned as such
constantString;

template literal or template which are delimited by backtick are used for.

  1. string interpolation
  2. exception function inline in string
  3. tagged template literal which are used to execute custom function
let a = 10;
console.log(`some string ${a}`)
console.log(`some string ${a+10}`)

a template literal is used to print out string to console while
template literal type are used for to represent a specific string value


This content originally appeared on DEV Community and was authored by omkar garde