Chuck's Academy

Basic TypeScript

Strict and Optional Typing in TypeScript

Strict typing is a key feature in TypeScript that ensures compliance with the typing rules defined by the developer, helping to prevent errors. In this chapter, we will delve into how strict and optional typing works in TypeScript, and how they can improve code robustness.

Strict Typing

Strict typing mode is a configuration option that you can enable in the tsconfig.json file. When enabled, it enforces stricter rules regarding the use of types. For example, it does not allow the use of null or undefined as values for types that do not expect them, nor the use of uninitialized variables.

To enable strict typing, the following option must be added or modified in the tsconfig.json file:

json
"This code enables the strict typing option in TypeScript, enforcing stricter rules regarding types."

Strict typing also includes other configurations, such as:

  • noImplicitAny: Prevents the use of implicit any, requiring the explicit declaration of a variable's type.
  • strictNullChecks: Does not allow assigning null or undefined to variables that do not expect them.
typescript
"In this example, the strictNullChecks option is enabled, so attempting to assign the value null to a variable of type number generates an error."

Optional Variables

In TypeScript, it is possible to define function parameters or object properties as optional. This means that it is not mandatory to provide a value for these variables. To do so, the ? symbol is used after the parameter or property name.

Optional Parameters

When a parameter is optional, the value may or may not be provided when the function is called. If not provided, its value will be undefined.

typescript
"In this example, the greet function has an optional parameter called name. If a name is provided, the function prints a personalized greeting; otherwise, it prints Hello, stranger."

Optional Properties

Object properties can also be optional, allowing some values to be absent when creating instances of a type.

typescript
"In this code, the user object has an optional property called email. The first object user1 does not have an email, while the second user2 does."

Type Assertions

Type assertions allow the developer to explicitly indicate to TypeScript that a variable has a specific type. This is useful when working with values whose type may be ambiguous or cannot be correctly inferred.

Type Assertion Syntax

There are two ways to make a type assertion in TypeScript:

  1. Using the as keyword:

    typescript
    "Here, someValue is of type any, but we are asserting that it is a string, so we can access the length property."
  2. Using angle bracket syntax:

    typescript
    "This example uses the same type assertion as the previous one, but with angle bracket syntax. Both forms are equivalent."

Conclusion

Strict and optional typing in TypeScript is essential for writing safer and clearer code. Through strict type rules and the use of optional parameters and type assertions, you can avoid common errors and have greater control over the data you handle.


Ask me anything