String literal and its difference compared to a string

A string is a type that allows any kind of characters. A string literal is the association of a particular string as a type. When a string is set to a type, it is possible to assign one value and change it later. The only value possible to set to a string literal is the exact string stamped at the declaration:

let x: string = "Value1";
x = "Value2";

let y: "Literal";
y = "Literal";
y = "sdasd"; // Won't compile

TypeScript compiles the code into plain JavaScript, without types. A string literal ensures that while writing the code in TypeScript that only a single string value can be associated with a variable, and this is compiled into JavaScript objects with this mandatory value. The essence of this particularity is that we have in both languages the assurance that the value is unique. This becomes handy in a situation where you need to condition a type that will not be there once compiled. For example, the case of an interface and a piece of code that must act differently depending on the interface. Having a shared field (with the name) among the interfaces with a unique string literal allow comparison at design time and run-time. At design time, TypeScript will be able to narrow down the type and hence provide better support for the specified type and at run-time be able to conduct the execution flow at the right place:

interface Book {
type: "book";
isbn: string;
page: number;
}

interface Movie {
type: "movie";
lengthMinutes: number;
}

let hobby: Movie = { type: "movie", lengthMinutes: 120 };

function showHobby(hobby: Book | Movie): void {
if (hobby.type === "movie") {
console.log("Long movie of " + hobby.lengthMinutes);
} else {
console.log("A book of few pages: " + hobby.page);
}
}

The code example shows that two interfaces share a type that is a string literal. To be able to access the unique property of one or the other interface in the function, a comparison of a discriminator is required. Without the comparison, the function that takes both interfaces with the union as a parameter does not know which of the two types is passed. However, TypeScript analyzes the two interfaces and identifies a common field and allows you to use this before scoping down the type. Once TypeScript can find which type is treated, it allows using the specific field of the type. In the example, inside the condition, all the movie's interface fields are available. On the other side, the else allows all the book's interface fields only.

A literal string is one type of three possible literals that TypeScript supports. TypeScript supports number and boolean on top of the string. Finally, when using string literal, always provide the type using the colon:

let myLiteral: "onlyAcceptedValue" = "onlyAcceptedValue";

Instead of relying on let, which opens the door to many assignations, the use of const can ensure a single assignation; hence, it will automatically infer a literal type for the three types:

const myLiteral = "onlyAcceptedValue"; // Not a string

It is possible to create a literal by omitting the type only if declared with const because the value cannot change; hence, TypeScript will scope to its most narrowed expression. However, a change from const to let in the future would change the type back to string. I suggest being as explicit as possible to avoid an undesired type of change.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.250.51