Usage of Extend , Read only , Enum, Literal Keywords in Typescript

Karthick S
2 min readApr 8, 2022

Extend: In situations where you want to create a type by building on top of an existing one, you can use an ampersand (&) to do that. 💪

Look at how we extend the Person type with additional props to emulate a database entry (PersonEntry). 🔥

Readonly<Type>: We can make objects immutable (properties can’t be changed) by simply applying the Readonly utility type.

In this example, we create an object of type persondetail and can still change its props.

In the second example(seconddata), we make it immutable. 👇

Enums are a useful type if you have a list of discrete values (e.g. directions, weekdays, month names, …).

Sometimes we want to get just the values as a new union string type.
With template literals, we can dynamically generate that. 👇

Template Literals : you can use string template literals to generate dynamically new interesting union types of string literals?

Let’s generate all possible word combinations using 2 predefined union types (Fruit and adjectives). 👇

Literal Types : You can use literal types to narrow down the allowed values even further.

When you combine it with the union type (pipe symbol) you can build interesting value checks.

Like in this example, the array will only accept these 4 values. 👇

--

--

Karthick S

Developer | Technical Writer | All things AWS | .Net