Open In App

How to Make a Generic Type Optional ?

Last Updated : 11 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In TypeScript, creating an optional generic type will make it optional to specify a type at the time of declaring the variables or parameters of that type.

These are the different approaches to making a generic type optional:

By assigning any type to the optional generic type

In this approach, we will assign the optional generic type with any type which makes it optional to specify the optional generic type.

Syntax:

type genericTypeName<T: string, S = any>;

Example: The below example will explain how you can make a generic type optional using any type.

Javascript




type optGen<T, S = any> = {
    name: T,
    desc: S
}
 
// Creating variable by specifying optional generic type
const genTypeSpecified: optGen<string, string> = {
    name: "GeeksforGeeks",
    desc: "A Computer Science Portal."
}
 
// Creating variable without
// specifying optional generic type
const genTypeNotSpecified: optGen<string> = {
    name: "GeeksforGeeks",
    desc: 500,
}
 
console.log(
    `Name: ${genTypeSpecified.name},
     Description: ${genTypeSpecified.desc}`);
console.log(
    `Name: ${genTypeNotSpecified.name},
    Workforce: ${genTypeNotSpecified.desc}`);


Output:

Name: GeeksforGeeks, Description: A Computer Science Portal.
Name: GeeksforGeeks, Workforce: 500

By assigning an empty object to the optional generic type

An empty object will make sure that there will be any non-nullish value can be assigned to this generic type whose type will be automatically assigned to this generic type if there is no type specified for that.

Syntax:

type genericTypeName<T: string, S = {}>;

Example: The below code example uses the empty object to make a generic type optional.

Javascript




type optGen<T, S = {}> = {
    name: T,
    desc: S
}
 
// Creating variable by specifying
// optional generic type
const genTypeSpecified: optGen<string, string> = {
    name: "GeeksforGeeks",
    desc: "A Computer Science Portal."
}
 
// Creating variable without
// specifying optional generic type
const genTypeNotSpecified: optGen<string> = {
    name: "GeeksforGeeks",
    desc: 500,
}
 
console.log(
    `Name: ${genTypeSpecified.name},
    Description: ${genTypeSpecified.desc}`);
console.log(
    `Name: ${genTypeNotSpecified.name},
    Workforce: ${genTypeNotSpecified.desc}`);


Output:

Name: GeeksforGeeks, Description: A Computer Science Portal. 
Name: GeeksforGeeks, Workforce: 500

By assigning undefined/void/never to the optional generic type

Assigning undefined or void or never types using union or individually to the optional generic type will also help you in making a generic type optional.

NOTE: In this case, if you haven’t specified the type for the generic type but assigned value to a property or variable using that generic will throw error. You either have to specify the type for the generic or don’t assign a value to properties or variables of that generic type.

Syntax:

type genericTypeName<T: string, S = undefined | void | never>;

Example: The below example will explain the use of these types in a union to make a generic type optional.

Javascript




type optGen<T, S = undefined | void | never> = {
    name: T,
    desc?: S
}
 
// Creating variable by specifying type
const genTypeSpecified: optGen<string, string> = {
    name: "GeeksforGeeks",
    desc: "A Computer Science Portal."
}
 
// Creating variable without specifying type
const genTypeNotSpecified: optGen<string> = {
    name: "GeeksforGeeks",
}
 
// Creating variable without specifying type
// But using property of that type
const genTypeNotSpecifiedWithUsedProp: optGen<string> = {
    name: "GeeksforGeeks",
    desc: 500,
}
 
console.log(
`Name: ${genTypeSpecified.name},
Description: ${genTypeSpecified.desc}`);
console.log(
`Name: ${genTypeNotSpecified.name}`);
console.log(
`Name: ${genTypeNotSpecifiedWithUsedProp.name}
Workforce: ${genTypeNotSpecifiedWithUsedProp.desc}`);


Output:

Name: GeeksforGeeks,  Description: A Computer Science Portal.
Name: GeeksforGeeks
Type 'number' is not assignable to type 'void'.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads