Grokking TypeScript
Vote

0% completed

​

Template Literal Types

Three actions combined with three entities give nine valid event names, and TypeScript can produce all nine from one line of type code.

type Action = "create" | "update" | "delete"; type Entity = "user" | "order" | "product"; type LogMessage = `${Action}_${Entity}`; // Equivalent to: // "create_user" | "create_order" | "create_product" // | "update_user" | "update_order" | "update_product" // | "delete_user" | "delete_order" | "delete_product"

The backtick syntax is the same as a JavaScript template literal, but here it builds a type instead of a string value

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content