Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2963

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T10:19:07+00:00 2024-11-26T10:19:07+00:00

Typescript – Tips & Tricks – Conditional Type

  • 61k

In some cases, we need to detect if an object/type has specific properties or characteristics.
Let me show you a simple case

type TypeName<T> =     T extends string ? "string" :     T extends number ? "number" :     T extends bigint ? "bigint" :     T extends boolean ? "boolean" :     T extends undefined ? "undefined" :     T extends symbol ? "symbol" :     T extends Function ? "function" :     T extends null ? "null" :     "object";  const typeName = <T>(obj: T): TypeName<T> => {   if (obj === null) return "null" as TypeName<T>;   return typeof obj as TypeName<T>; };  const stringType = typeName('string'); // "string" const numberType = typeName(10); // "number" const bigIntType = typeName(10n); // "bigint" const booleanType = typeName(true); // "boolean" const undefinedType = typeName(undefined); // "undefined" const symbolType = typeName(Symbol()); // "symbol" const functionType = typeName(() => 'result'); // "function" const nullType = typeName(null); // "null" const objectType = typeName({ name: 'name'}); // "object" 
Enter fullscreen mode Exit fullscreen mode

In this example, we return the type of the parameter as a string, but I want to focus on the TypeName type. This type as you can see, returns the values based on some conditions. In this case, the conditions are based on the parameter's type, but my goal is to show you that you could check if a type respects some conditions or not and act accordingly.
This feature is called Conditional Types.
To show you the power of this feature here a funny example found in the net 🙂

type SnackBars = {   name: "Short Chocolate Bars";   amount: 4;   candy: true; };  type Gumballs = {   name: "Gooey Gumballs";   color: "green" | "purples";   candy: true; };  type Apples = {   name: "Apples";   candy: true; };  type Cookies = {   name: "Cookies";   candy: true;   peanuts: true; };  type SnickersBar = {   name: "Snickers Bar";   candy: true;   peanuts: true; };  type Toothpaste = {   name: "Toothpaste";   minty: true;   trick: true; };  type Pencil = {   name: "Pencil";   trick: true; };  type HalloweenTricksAndSweets =   | SnackBars   | Gumballs   | Apples   | SnickersBar   | Cookies   | Toothpaste   | Pencil;  type AllCandies<T> = T extends { candy: true } ? T : never; type AllTricks<T> = T extends { trick: true } ? T : never; type AllCandiesWithoutPeanuts<T> = T extends { candy: true } ? (T extends { peanuts: true } ? never : T) : never;  type Candies = AllCandies<HalloweenTricksAndSweets>; // SnackBars | Gumballs | Apples | SnickersBar | Cookies type Tricks = AllTricks<HalloweenTricksAndSweets>; // Toothpaste | Pencil type CandiesWithoutPeanuts = AllCandiesWithoutPeanuts<HalloweenTricksAndSweets>; // SnackBars | Gumballs | Apples 
Enter fullscreen mode Exit fullscreen mode

In this example, you can see how the AllCandies type creates a new type (Candies) composed of all the HalloweenTricksAndSweets that contain the candy property with the true value. The AllTricks type creates a new type (Tricks) composed of all the HalloweenTricksAndSweets that contain the trick property with the true value. The last case AllCandiesWithoutPeanuts is interesting: we get all the candies that don't contain the peanuts property with the true value.
I think this funny example can express best the potential of the Conditional Type, and it can help you to understand better all the benefits it can lead to.

It's all for today.
I hope this feature will help you in the future as it helped me in some cases.

See you soon Guys!

typescriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.