DEV Community

omkar garde
omkar garde

Posted on

what are template literals types and what are template literals?

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;
Enter fullscreen mode Exit fullscreen mode

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}`)
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)