Open In App

How to Define a List of Optional Keys for Typescript Record ?

Last Updated : 10 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A Record is a utility type used to define objects with specific key-value pairs. In this article, we will explore two approaches to Define a list of optional keys for Typescript Record.

Using Partial<Type> for Optional Keys

We use TypeScript’s Partial<Type> to define optional keys in the obj type related to GeeksforGeeks, where opKeys specifies the specific optional keys like ‘title’, ‘author’, ‘likes’, ‘published’, and ‘category’, allowing them to have string, number, or boolean values.

Example: The below example uses Partial<Type> for Optional Keys to Define a list of optional keys for Typescript Record.

JavaScript
type opKeys = 'title' | 'author' | 'likes' | 'published' | 'category';

type obj = Partial<Record<opKeys, string | number | boolean>>;

const data: obj = {
    title: 'GFG',
    author: 'GeeksforGeeks',
    likes: 100,
    published: true,
    category: 'Programming',
};

console.log(data);

Output:

{
"title": "GFG",
"author": "GeeksforGeeks",
"likes": 100,
"published": true,
"category": "Programming"
}

Using conditional types with keyof and Partial<Type>

We use conditional types in TypeScript, specifically Partial combined with Record<K, T[K]>, to define a list of optional keys (PartialKeys) for an object type (obj). This approach allows us to make only specified keys optional while retaining their original types.

Example: The below example uses conditional types with keyof and Partial<Type> to Define a list of optional keys for Typescript Record.

JavaScript
type obj = {
    title: string;
    author: string;
    likes: number;
    published: boolean;
    category: string;
};

type PartialKeys<T, K extends keyof T> =
    Partial<Record<K, T[K]>>;

type PartialObj = PartialKeys<obj, keyof obj>;

const data: PartialObj = {
    title: 'GFG',
    author: 'GeeksforGeeks',
    likes: 100,
};

console.log(data);

Output:

{
"title": "GFG",
"author": "GeeksforGeeks",
"likes": 100
}

Using Dynamic Key Assignment with Intersection Types

Rather than defining optional keys statically or based on specific conditions, we can create a function that dynamically determines which keys to include in the resulting object by intersecting the original object type with a type that includes optional keys.

Example:

JavaScript
// Define the object type
type Obj = {
    title: string;
    author: string;
    likes: number;
    published: boolean;
    category: string;
};

// Define a function to dynamically generate an object with optional keys
function generateOptionalData<T extends Record<string, any>>(
    data: T,
    optionalKeys: Partial<T>
): T & Partial<T> {
    return { ...data, ...optionalKeys };
}

// Use the function to generate optional keys dynamically
const data: Obj = {
    title: 'GFG',
    author: 'Anonymous',
    likes: 100,
    published: true,
    category: 'Programming',
};

// Define optional keys based on certain conditions
const optionalKeys = {
    ...(data.author === 'Anonymous' ? { likes: undefined } : {}),
    // Add more conditions and optional keys as needed
};

const optionalData = generateOptionalData(data, optionalKeys);

console.log(optionalData);

Output:

{
"title": "GFG",
"author": "Anonymous",
"likes": undefined,
"published": true,
"category": "Programming"
}


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads