<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Omotoso Abosede Racheal</title>
    <description>The latest articles on DEV Community by Omotoso Abosede Racheal (@rachealcloud).</description>
    <link>https://dev.to/rachealcloud</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1611690%2F3efe9cf5-7ad5-4751-bd97-173fd0e88be1.jpeg</url>
      <title>DEV Community: Omotoso Abosede Racheal</title>
      <link>https://dev.to/rachealcloud</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rachealcloud"/>
    <language>en</language>
    <item>
      <title>Conditional Types in TypeScript.</title>
      <dc:creator>Omotoso Abosede Racheal</dc:creator>
      <pubDate>Fri, 26 Jul 2024 11:23:50 +0000</pubDate>
      <link>https://dev.to/rachealcloud/conditional-types-in-typescript-1hek</link>
      <guid>https://dev.to/rachealcloud/conditional-types-in-typescript-1hek</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Conditional types in TypeScript allow you to express type relationships using a form of ternary logic at the type level. They provide a way to perform type checks and return different types based on those checks. The syntax and basic usage of conditional types are similar to JavaScript's ternary operator.&lt;/p&gt;

&lt;p&gt;Conditional types help describe the relation between the types of inputs and outputs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T extend U ? X : Y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Prerequisite
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Javascript&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What you will learn
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Some Conditional types examples&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Conditional Type Constraint&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inferring Within Conditional Types&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Distributive Conditional Types&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Some Conditional types examples
&lt;/h3&gt;

&lt;p&gt;Conditional types take a form that looks a little like conditional expressions (condition ? trueExpression : falseExpression) in JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SomeType extends OtherType ? TrueType : FalseType;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the type on the left of the extends is assignable to the one on the right, then you’ll get the type in the first branch (the “true” branch); otherwise, you’ll get the type in the latter branch (the “false” branch).&lt;/p&gt;

&lt;p&gt;let’s take another example for a createLabel function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface IdLabel {
  id: number /* some fields */;
}
interface NameLabel {
  name: string /* other fields */;
}

function createLabel(id: number): IdLabel;
function createLabel(name: string): NameLabel;
function createLabel(nameOrId: string | number): IdLabel | NameLabel;
function createLabel(nameOrId: string | number): IdLabel | NameLabel {
  throw "unimplemented";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These overloads for createLabel describe a single JavaScript function that makes a choice based on the types of its inputs. Note a few things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If a library has to make the same sort of choice over and over throughout its API, this becomes cumbersome.&lt;/li&gt;
&lt;li&gt;We have to create three overloads: one for each case when we’re sure of the type (one for string and one for number), and one for the most general case (taking a string | number). For every new type createLabel can handle, the number of overloads grows exponentially.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead, we can encode that logic in a conditional type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type NameOrId&amp;lt;T extends number | string&amp;gt; = T extends number
  ? IdLabel
  : NameLabel;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can then use that conditional type to simplify our overloads down to a single function with no overloads.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createLabel&amp;lt;T extends number | string&amp;gt;(idOrName: T): NameOrId&amp;lt;T&amp;gt; {
  throw "unimplemented";
}

let a = createLabel("typescript");

let a: NameLabel

let b = createLabel(2.8);

let b: IdLabel

let c = createLabel(Math.random() ? "hello" : 42);
let c: NameLabel | IdLabel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conditional Type Constraint
&lt;/h3&gt;

&lt;p&gt;In TypeScript, a conditional type constraint is a way to impose restrictions (constraint) on type parameters in generic types using conditional types. This technique allows you to express more complex type relationships and ensure that type parameters meet certain criteria.&lt;/p&gt;

&lt;p&gt;Let's start with a basic example to illustrate how conditional type constraints work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type CheckType&amp;lt;T&amp;gt; = T extends string ? "String Type" : "Other Type";

type A = CheckType&amp;lt;string&amp;gt;;  // "String Type"
type B = CheckType&amp;lt;number&amp;gt;;  // "Other Type"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, CheckType is a conditional type that checks if the type T extends string. If it does, it returns "String Type"; otherwise, it returns "Other Type".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Conditional Type Constraints in Generics&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Conditional type constraints are often used within generic types to enforce more specific type rules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Constraining Function Parameters&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's an example where a conditional type constraint ensures that a function parameter must be a specific type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type EnsureString&amp;lt;T&amp;gt; = T extends string ? T : never;

function logString&amp;lt;T&amp;gt;(value: EnsureString&amp;lt;T&amp;gt;): void {
  console.log(value);
}

logString("Hello"); // Works
logString(42);   // Error: Argument of type '42' is not assignable to parameter of type 'never'.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the EnsureString type ensures that T must be a string. If T is not a string, the type becomes never, which is an uninhabitable type and causes a type error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;More Complex Example: Constraining Based on Object Properties&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can also use conditional type constraints to enforce more complex rules, such as ensuring an object has specific properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type HasName&amp;lt;T&amp;gt; = T extends { name: string } ? T : never;

function greet&amp;lt;T&amp;gt;(obj: HasName&amp;lt;T&amp;gt;): void {
  console.log(`Hello, ${obj.name}`);
}

greet({ name: "Alice" });       // Works
greet({ name: "Bob", age: 30 }); // Works
greet({ age: 30 });             // Error: Argument of type '{ age: number; }' is not assignable to parameter of type 'never'.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the HasName type ensures that T must be an object with a name property of type string. If T does not have this property, the type becomes never, causing a type error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inferring Within Conditional Types
&lt;/h3&gt;

&lt;p&gt;In TypeScript, you can use the infer keyword within conditional types to introduce a type variable that can be inferred from a specific part of a type. This is particularly useful when working with complex types such as functions, tuples, and arrays, where you might want to extract a particular type of component.&lt;/p&gt;

&lt;p&gt;The syntax for using infer within a conditional type is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type SomeType&amp;lt;T&amp;gt; = T extends SomeCondition ? infer U : FallbackType;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, infer U allows TypeScript to infer the type U from T if T satisfies SomeCondition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Extracting the Return Type of a Function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can use infer to extract the return type of a function type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type GetReturnType&amp;lt;T&amp;gt; = T extends (...args: any[]) =&amp;gt; infer R ? R : never;

type Func = (a: number, b: string) =&amp;gt; boolean;
type ReturnTypeOfFunc = GetReturnType&amp;lt;Func&amp;gt;;  // boolean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, GetReturnType checks if T is a function type. If it is, infer R captures the return type of the function, and GetReturnType resolves to that return type (R). Otherwise, it resolves to never.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Extracting the Element Type of an Array&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can use infer to extract the type of elements in an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type ElementType&amp;lt;T&amp;gt; = T extends (infer U)[] ? U : never;

type ArrayOfNumbers = number[];
type NumberElementType = ElementType&amp;lt;ArrayOfNumbers&amp;gt;;  // number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, ElementType checks if T is an array type. If it is, infer U captures the element type, and ElementType resolves to that element type (U). Otherwise, it resolves to never.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Extracting Tuple Types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can use infer to work with tuple types and extract their component types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type FirstElement&amp;lt;T&amp;gt; = T extends [infer U, ...any[]] ? U : never;

type Tuple = [string, number, boolean];
type FirstType = FirstElement&amp;lt;Tuple&amp;gt;;  // string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, FirstElement checks if T is a tuple type. If it is, infer U captures the type of the first element, and FirstElement resolves to that type (U). Otherwise, it resolves to never.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advanced Example: Extracting Parameter Types of a Function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can use infer to extract the types of the parameters of a function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type GetParameters&amp;lt;T&amp;gt; = T extends (...args: infer P) =&amp;gt; any ? P : never;

type Func = (a: number, b: string) =&amp;gt; void;
type ParametersOfFunc = GetParameters&amp;lt;Func&amp;gt;;  // [number, string]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, GetParameters checks if T is a function type. If it is, infer P captures the parameter types as a tuple, and GetParameters resolves to that tuple type (P). Otherwise, it resolves to never.&lt;/p&gt;

&lt;h3&gt;
  
  
  Distributive Conditional Types
&lt;/h3&gt;

&lt;p&gt;When conditional types act on a generic type, they become distributive when given a union type. This means that if you have a conditional type T and T is a union, the conditional type will be applied to each member of the union. &lt;br&gt;
For example, take the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type ToArray&amp;lt;Type&amp;gt; = Type extends any ? Type[] : never;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we plug a union type into ToArray, then the conditional type will be applied to each member of that union.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type ToArray&amp;lt;Type&amp;gt; = Type extends any ? Type[] : never;

type StrArrOrNumArr = ToArray&amp;lt;string | number&amp;gt;;

type StrArrOrNumArr = string[] | number[]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens here is that ToArray distributes on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; string | number;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and maps over each member type of the union, to what is effectively:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ToArray&amp;lt;string&amp;gt; | ToArray&amp;lt;number&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which leaves us with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string[] | number[];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Typically, distributivity is the desired behavior. To avoid that behavior, you can surround each side of the extends keyword with square brackets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type ToArrayNonDist&amp;lt;Type&amp;gt; = [Type] extends [any] ? Type[] : never;

// 'ArrOfStrOrNum' is no longer a union.
type ArrOfStrOrNum = ToArrayNonDist&amp;lt;string | number&amp;gt;;

type ArrOfStrOrNum = (string | number)[]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Conditional types in Typescript are powerful feature that enables advanced type manipulation and can help create more precise and flexible type definitions. They are particularly useful in generic programming and when working with complex type transformation.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>react</category>
      <category>opensource</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Conditional Types in TypeScript.</title>
      <dc:creator>Omotoso Abosede Racheal</dc:creator>
      <pubDate>Fri, 26 Jul 2024 11:23:50 +0000</pubDate>
      <link>https://dev.to/rachealcloud/conditional-types-in-typescript-1fgc</link>
      <guid>https://dev.to/rachealcloud/conditional-types-in-typescript-1fgc</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Conditional types in TypeScript allow you to express type relationships using a form of ternary logic at the type level. They provide a way to perform type checks and return different types based on those checks. The syntax and basic usage of conditional types are similar to JavaScript's ternary operator.&lt;/p&gt;

&lt;p&gt;Conditional types help describe the relation between the types of inputs and outputs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T extend U ? X : Y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Prerequisite
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Javascript&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What you will learn
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Some Conditional types examples&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Conditional Type Constraint&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inferring Within Conditional Types&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Distributive Conditional Types&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Some Conditional types examples
&lt;/h3&gt;

&lt;p&gt;Conditional types take a form that looks a little like conditional expressions (condition ? trueExpression : falseExpression) in JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SomeType extends OtherType ? TrueType : FalseType;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the type on the left of the extends is assignable to the one on the right, then you’ll get the type in the first branch (the “true” branch); otherwise, you’ll get the type in the latter branch (the “false” branch).&lt;/p&gt;

&lt;p&gt;let’s take another example for a createLabel function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface IdLabel {
  id: number /* some fields */;
}
interface NameLabel {
  name: string /* other fields */;
}

function createLabel(id: number): IdLabel;
function createLabel(name: string): NameLabel;
function createLabel(nameOrId: string | number): IdLabel | NameLabel;
function createLabel(nameOrId: string | number): IdLabel | NameLabel {
  throw "unimplemented";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These overloads for createLabel describe a single JavaScript function that makes a choice based on the types of its inputs. Note a few things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If a library has to make the same sort of choice over and over throughout its API, this becomes cumbersome.&lt;/li&gt;
&lt;li&gt;We have to create three overloads: one for each case when we’re sure of the type (one for string and one for number), and one for the most general case (taking a string | number). For every new type createLabel can handle, the number of overloads grows exponentially.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead, we can encode that logic in a conditional type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type NameOrId&amp;lt;T extends number | string&amp;gt; = T extends number
  ? IdLabel
  : NameLabel;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can then use that conditional type to simplify our overloads down to a single function with no overloads.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createLabel&amp;lt;T extends number | string&amp;gt;(idOrName: T): NameOrId&amp;lt;T&amp;gt; {
  throw "unimplemented";
}

let a = createLabel("typescript");

let a: NameLabel

let b = createLabel(2.8);

let b: IdLabel

let c = createLabel(Math.random() ? "hello" : 42);
let c: NameLabel | IdLabel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conditional Type Constraint
&lt;/h3&gt;

&lt;p&gt;In TypeScript, a conditional type constraint is a way to impose restrictions (constraint) on type parameters in generic types using conditional types. This technique allows you to express more complex type relationships and ensure that type parameters meet certain criteria.&lt;/p&gt;

&lt;p&gt;Let's start with a basic example to illustrate how conditional type constraints work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type CheckType&amp;lt;T&amp;gt; = T extends string ? "String Type" : "Other Type";

type A = CheckType&amp;lt;string&amp;gt;;  // "String Type"
type B = CheckType&amp;lt;number&amp;gt;;  // "Other Type"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, CheckType is a conditional type that checks if the type T extends string. If it does, it returns "String Type"; otherwise, it returns "Other Type".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Conditional Type Constraints in Generics&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Conditional type constraints are often used within generic types to enforce more specific type rules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Constraining Function Parameters&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's an example where a conditional type constraint ensures that a function parameter must be a specific type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type EnsureString&amp;lt;T&amp;gt; = T extends string ? T : never;

function logString&amp;lt;T&amp;gt;(value: EnsureString&amp;lt;T&amp;gt;): void {
  console.log(value);
}

logString("Hello"); // Works
logString(42);   // Error: Argument of type '42' is not assignable to parameter of type 'never'.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the EnsureString type ensures that T must be a string. If T is not a string, the type becomes never, which is an uninhabitable type and causes a type error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;More Complex Example: Constraining Based on Object Properties&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can also use conditional type constraints to enforce more complex rules, such as ensuring an object has specific properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type HasName&amp;lt;T&amp;gt; = T extends { name: string } ? T : never;

function greet&amp;lt;T&amp;gt;(obj: HasName&amp;lt;T&amp;gt;): void {
  console.log(`Hello, ${obj.name}`);
}

greet({ name: "Alice" });       // Works
greet({ name: "Bob", age: 30 }); // Works
greet({ age: 30 });             // Error: Argument of type '{ age: number; }' is not assignable to parameter of type 'never'.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the HasName type ensures that T must be an object with a name property of type string. If T does not have this property, the type becomes never, causing a type error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inferring Within Conditional Types
&lt;/h3&gt;

&lt;p&gt;In TypeScript, you can use the infer keyword within conditional types to introduce a type variable that can be inferred from a specific part of a type. This is particularly useful when working with complex types such as functions, tuples, and arrays, where you might want to extract a particular type of component.&lt;/p&gt;

&lt;p&gt;The syntax for using infer within a conditional type is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type SomeType&amp;lt;T&amp;gt; = T extends SomeCondition ? infer U : FallbackType;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, infer U allows TypeScript to infer the type U from T if T satisfies SomeCondition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Extracting the Return Type of a Function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can use infer to extract the return type of a function type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type GetReturnType&amp;lt;T&amp;gt; = T extends (...args: any[]) =&amp;gt; infer R ? R : never;

type Func = (a: number, b: string) =&amp;gt; boolean;
type ReturnTypeOfFunc = GetReturnType&amp;lt;Func&amp;gt;;  // boolean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, GetReturnType checks if T is a function type. If it is, infer R captures the return type of the function, and GetReturnType resolves to that return type (R). Otherwise, it resolves to never.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Extracting the Element Type of an Array&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can use infer to extract the type of elements in an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type ElementType&amp;lt;T&amp;gt; = T extends (infer U)[] ? U : never;

type ArrayOfNumbers = number[];
type NumberElementType = ElementType&amp;lt;ArrayOfNumbers&amp;gt;;  // number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, ElementType checks if T is an array type. If it is, infer U captures the element type, and ElementType resolves to that element type (U). Otherwise, it resolves to never.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Extracting Tuple Types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can use infer to work with tuple types and extract their component types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type FirstElement&amp;lt;T&amp;gt; = T extends [infer U, ...any[]] ? U : never;

type Tuple = [string, number, boolean];
type FirstType = FirstElement&amp;lt;Tuple&amp;gt;;  // string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, FirstElement checks if T is a tuple type. If it is, infer U captures the type of the first element, and FirstElement resolves to that type (U). Otherwise, it resolves to never.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advanced Example: Extracting Parameter Types of a Function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can use infer to extract the types of the parameters of a function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type GetParameters&amp;lt;T&amp;gt; = T extends (...args: infer P) =&amp;gt; any ? P : never;

type Func = (a: number, b: string) =&amp;gt; void;
type ParametersOfFunc = GetParameters&amp;lt;Func&amp;gt;;  // [number, string]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, GetParameters checks if T is a function type. If it is, infer P captures the parameter types as a tuple, and GetParameters resolves to that tuple type (P). Otherwise, it resolves to never.&lt;/p&gt;

&lt;h3&gt;
  
  
  Distributive Conditional Types
&lt;/h3&gt;

&lt;p&gt;When conditional types act on a generic type, they become distributive when given a union type. This means that if you have a conditional type T and T is a union, the conditional type will be applied to each member of the union. &lt;br&gt;
For example, take the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type ToArray&amp;lt;Type&amp;gt; = Type extends any ? Type[] : never;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we plug a union type into ToArray, then the conditional type will be applied to each member of that union.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type ToArray&amp;lt;Type&amp;gt; = Type extends any ? Type[] : never;

type StrArrOrNumArr = ToArray&amp;lt;string | number&amp;gt;;

type StrArrOrNumArr = string[] | number[]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens here is that ToArray distributes on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; string | number;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and maps over each member type of the union, to what is effectively:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ToArray&amp;lt;string&amp;gt; | ToArray&amp;lt;number&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which leaves us with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string[] | number[];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Typically, distributivity is the desired behavior. To avoid that behavior, you can surround each side of the extends keyword with square brackets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type ToArrayNonDist&amp;lt;Type&amp;gt; = [Type] extends [any] ? Type[] : never;

// 'ArrOfStrOrNum' is no longer a union.
type ArrOfStrOrNum = ToArrayNonDist&amp;lt;string | number&amp;gt;;

type ArrOfStrOrNum = (string | number)[]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Conditional types in Typescript are powerful feature that enables advanced type manipulation and can help create more precise and flexible type definitions. They are particularly useful in generic programming and when working with complex type transformation.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>react</category>
      <category>opensource</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Conditional Rendering in React</title>
      <dc:creator>Omotoso Abosede Racheal</dc:creator>
      <pubDate>Wed, 12 Jun 2024 10:13:40 +0000</pubDate>
      <link>https://dev.to/rachealcloud/conditional-rendering-in-react-1lmi</link>
      <guid>https://dev.to/rachealcloud/conditional-rendering-in-react-1lmi</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Conditional Rendering in React refers to the technique of displaying components or element based on certain conditions. Your components will often need to display different things depending on different conditions. In React, you can conditionally render JSX using JavaScript syntax like if statements, &amp;amp;&amp;amp;, and ? : operators.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisite&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;HTML&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Javascript&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What you will learn&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;How to return different JSX depending on a condition&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How to conditionally include or exclude a piece of JSX&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Common conditional syntax shortcuts you’ll encounter in React codebases&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How to return different JSX depending on a condition&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s say you have a PackingList component rendering several Items, which can be marked as packed or not:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Item({ name, isPacked }) {
  return &amp;lt;li className="item"&amp;gt;{name}&amp;lt;/li&amp;gt;;
}

export default function PackingList() {
  return (
    &amp;lt;section&amp;gt;
      &amp;lt;h1&amp;gt;Sally Ride's Packing List&amp;lt;/h1&amp;gt;
      &amp;lt;ul&amp;gt;
        &amp;lt;Item 
          isPacked={true} 
          name="Space suit" 
        /&amp;gt;
        &amp;lt;Item 
          isPacked={true} 
          name="Helmet with a golden leaf" 
        /&amp;gt;
        &amp;lt;Item 
          isPacked={false} 
          name="Photo of Tam" 
        /&amp;gt;
      &amp;lt;/ul&amp;gt;
    &amp;lt;/section&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that some of the Item components have their isPacked prop set to true instead of false. You want to add a checkmark (✔) to packed items if isPacked={true}.&lt;/p&gt;

&lt;p&gt;You can write this as an if/else statement like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (isPacked) {
  return &amp;lt;li className="item"&amp;gt;{name} ✔&amp;lt;/li&amp;gt;;
}
return &amp;lt;li className="item"&amp;gt;{name}&amp;lt;/li&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the isPacked prop is true, this code returns a different JSX tree. With this change, some of the items get a checkmark at the end:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Item({ name, isPacked }) {
  if (isPacked) {
    return &amp;lt;li className="item"&amp;gt;{name} ✔&amp;lt;/li&amp;gt;;
  }
  return &amp;lt;li className="item"&amp;gt;{name}&amp;lt;/li&amp;gt;;
}

export default function PackingList() {
  return (
    &amp;lt;section&amp;gt;
      &amp;lt;h1&amp;gt;Sally Ride's Packing List&amp;lt;/h1&amp;gt;
      &amp;lt;ul&amp;gt;
        &amp;lt;Item 
          isPacked={true} 
          name="Space suit" 
        /&amp;gt;
        &amp;lt;Item 
          isPacked={true} 
          name="Helmet with a golden leaf" 
        /&amp;gt;
        &amp;lt;Item 
          isPacked={false} 
          name="Photo of Tam" 
        /&amp;gt;
      &amp;lt;/ul&amp;gt;
    &amp;lt;/section&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conditionally including JSX&lt;/strong&gt;&lt;br&gt;
In the previous example, you controlled which (if any!) JSX tree would be returned by the component. You may already have noticed some duplication in the render output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;li className="item"&amp;gt;{name} ✔&amp;lt;/li&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is very similar to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;li className="item"&amp;gt;{name}&amp;lt;/li&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both of the conditional branches return&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;li className="item"&amp;gt;...&amp;lt;/li&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (isPacked) {
  return &amp;lt;li className="item"&amp;gt;{name} ✔&amp;lt;/li&amp;gt;;
}
return &amp;lt;li className="item"&amp;gt;{name}&amp;lt;/li&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While this duplication isn’t harmful, it could make your code harder to maintain. What if you want to change the className? You’d have to do it in two places in your code! In such a situation, you could conditionally include a little JSX to make your code more DRY.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Conditional (ternary) operator (? :)&lt;/em&gt; &lt;br&gt;
JavaScript has a compact syntax for writing a conditional expression — the conditional operator or “ternary operator”.&lt;/p&gt;

&lt;p&gt;Instead of this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (isPacked) {
  return &amp;lt;li className="item"&amp;gt;{name} ✔&amp;lt;/li&amp;gt;;
}
return &amp;lt;li className="item"&amp;gt;{name}&amp;lt;/li&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can write this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return (
  &amp;lt;li className="item"&amp;gt;
    {isPacked ? name + ' ✔' : name}
  &amp;lt;/li&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can read it as “if isPacked is true, then (?) render name + ' ✔', otherwise (:) render name”.&lt;/p&gt;

&lt;p&gt;Now let’s say you want to wrap the completed item’s text into another HTML tag, like 'del' to strike it out. You can add even more newlines and parentheses so that it’s easier to nest more JSX in each of the cases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Item({ name, isPacked }) {
  return (
    &amp;lt;li className="item"&amp;gt;
      {isPacked ? (
        &amp;lt;del&amp;gt;
          {name + ' ✔'}
        &amp;lt;/del&amp;gt;
      ) : (
        name
      )}
    &amp;lt;/li&amp;gt;
  );
}

export default function PackingList() {
  return (
    &amp;lt;section&amp;gt;
      &amp;lt;h1&amp;gt;Sally Ride's Packing List&amp;lt;/h1&amp;gt;
      &amp;lt;ul&amp;gt;
        &amp;lt;Item 
          isPacked={true} 
          name="Space suit" 
        /&amp;gt;
        &amp;lt;Item 
          isPacked={true} 
          name="Helmet with a golden leaf" 
        /&amp;gt;
        &amp;lt;Item 
          isPacked={false} 
          name="Photo of Tam" 
        /&amp;gt;
      &amp;lt;/ul&amp;gt;
    &amp;lt;/section&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Logical AND operator (&amp;amp;&amp;amp;)&lt;/em&gt;&lt;br&gt;
Another common shortcut you’ll encounter is the JavaScript logical AND (&amp;amp;&amp;amp;) operator. Inside React components, it often comes up when you want to render some JSX when the condition is true, or render nothing otherwise. With &amp;amp;&amp;amp;, you could conditionally render the checkmark only if isPacked is true&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return (
  &amp;lt;li className="item"&amp;gt;
    {name} {isPacked &amp;amp;&amp;amp; '✔'}
  &amp;lt;/li&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can read this as “if isPacked, then (&amp;amp;&amp;amp;) render the checkmark, otherwise, render nothing”.&lt;/p&gt;

&lt;p&gt;Here it is in action&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Item({ name, isPacked }) {
  return (
    &amp;lt;li className="item"&amp;gt;
      {name} {isPacked &amp;amp;&amp;amp; '✔'}
    &amp;lt;/li&amp;gt;
  );
}

export default function PackingList() {
  return (
    &amp;lt;section&amp;gt;
      &amp;lt;h1&amp;gt;Sally Ride's Packing List&amp;lt;/h1&amp;gt;
      &amp;lt;ul&amp;gt;
        &amp;lt;Item 
          isPacked={true} 
          name="Space suit" 
        /&amp;gt;
        &amp;lt;Item 
          isPacked={true} 
          name="Helmet with a golden leaf" 
        /&amp;gt;
        &amp;lt;Item 
          isPacked={false} 
          name="Photo of Tam" 
        /&amp;gt;
      &amp;lt;/ul&amp;gt;
    &amp;lt;/section&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A JavaScript &amp;amp;&amp;amp; expression returns the value of its right side (in our case, the checkmark) if the left side (our condition) is true. But if the condition is false, the whole expression becomes false. React considers false as a “hole” in the JSX tree, just like null or undefined, and doesn’t render anything in its place.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conditionally assigning JSX to a variable&lt;/strong&gt;&lt;br&gt;
When the shortcuts get in the way of writing plain code, try using an if statement and a variable. You can reassign variables defined with let, so start by providing the default content you want to display, the name&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let itemContent = name;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use an if statement to reassign a JSX expression to itemContent if isPacked is true:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (isPacked) {
  itemContent = name + " ✔";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This style is the most verbose, but it’s also the most flexible. Here it is in action&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Item({ name, isPacked }) {
  let itemContent = name;
  if (isPacked) {
    itemContent = name + " ✔";
  }
  return (
    &amp;lt;li className="item"&amp;gt;
      {itemContent}
    &amp;lt;/li&amp;gt;
  );
}

export default function PackingList() {
  return (
    &amp;lt;section&amp;gt;
      &amp;lt;h1&amp;gt;Sally Ride's Packing List&amp;lt;/h1&amp;gt;
      &amp;lt;ul&amp;gt;
        &amp;lt;Item 
          isPacked={true} 
          name="Space suit" 
        /&amp;gt;
        &amp;lt;Item 
          isPacked={true} 
          name="Helmet with a golden leaf" 
        /&amp;gt;
        &amp;lt;Item 
          isPacked={false} 
          name="Photo of Tam" 
        /&amp;gt;
      &amp;lt;/ul&amp;gt;
    &amp;lt;/section&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you’re not familiar with JavaScript, this variety of styles might seem overwhelming at first. However, learning them will help you read and write any JavaScript code — and not just React components! Pick the one you prefer for a start, and then consult this reference again if you forget how the other ones work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In React, you control branching logic with JavaScript.&lt;br&gt;
You can return a JSX expression conditionally with an if statement.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can conditionally save some JSX to a variable and then include it inside other JSX by using the curly braces.&lt;br&gt;
In JSX, {cond ? &lt;a&gt;&lt;/a&gt; : &lt;b&gt;&lt;/b&gt;} means “if cond, render &lt;a&gt;&lt;/a&gt;, otherwise &lt;b&gt;&lt;/b&gt;”.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In JSX, {cond &amp;amp;&amp;amp; &lt;a&gt;&lt;/a&gt;} means “if cond, render &lt;a&gt;&lt;/a&gt;, otherwise nothing”.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The shortcuts are common, but you don’t have to use them if you prefer plain if.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>react</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
