Const assertions in literal expressions in TypeScript

Click for: original source

With TypeScript 3.4, const assertions were added to the language. A const assertion is a special kind of type assertion in which the const keyword is used instead of a type name. By Marius Schulz.

TypeScript has a special kind of literal type that’s known as a non-widening literal type. As the name suggests, non-widening literal types will not be widened to a more generic type. For example, the non-widening string literal type “GET” will not be widened to string in cases where type widening would normally occur.

The article dives straight in:

  • Motivation for const Assertions
  • String Literal Types
  • Literal Type Widening
  • Non-Widening Literal Types
  • const Assertions for Literal Expressions
  • Using TypeScript Enums
  • const Assertions for Other Types

Plenty of code examples in this to the point article!

[Read More]

Tags javascript nodejs programming web-development