Posts

Showing posts from April, 2024

Understanding the Use of type in TypeScript

Image
In TypeScript , using the type keyword is a fundamental way to define custom data types. The type declaration is one of the main features that distinguish TypeScript from JavaScript , as it allows you to explicitly specify the types of variables, functions, objects, and more. This not only aids in code development and maintenance but also enhances autocomplete features and compile-time checks, reducing runtime errors. How to Use type You can use the keyword type to create a type alias that can be reused in various places in your code. A type alias can be primitive, a union, an intersection, a literal type, and more. Basic Example: In this example, Name is a type representing a string. Whenever you need a variable that should store a name, you can use the type Name instead of string , making the code more readable and manageable. Unions: Intersection: Literal Types: Why Use type Readability: Using type makes the code more readable. Type names can describe what the variable rep...

Parameters "arr", "not", and "orr" in TypeScript

Image
The parameters arr , not , and orr play an essential role in TypeScript 's infer function, allowing for fine-grained control over how types are inferred. Parameter arr : Description: This parameter specifies whether the inferred type should be an array. Possible values: true: Indicates that the inferred type will be an array. false: Indicates that the inferred type will not be an array. Examples: Parameter not: Description: This parameter specifies that the inferred type cannot be the specified type. Possible values: The type that should not be inferred. Examples: Parameter orr : Description: This parameter specifies that the inferred type must be one of the specified types. Possible values: A list of types. Examples: Notes: The parameters arr , not , and orr can be used together. The order of the parameters is not relevant. These parameters provide powerful tools for controlling how TypeScript infers types, allowing for precise and flexible type definitions in your code....