Skip to content

Instantly share code, notes, and snippets.

@psahni
Last active January 16, 2026 07:16
Show Gist options
  • Select an option

  • Save psahni/33187332d77553119c0827f0145c25d8 to your computer and use it in GitHub Desktop.

Select an option

Save psahni/33187332d77553119c0827f0145c25d8 to your computer and use it in GitHub Desktop.
Tip: TypeScript Utility Types

Tip: TypeScript Utility Types Suppose you have a javascript object but the object is dynamic, except few properties, you don't know what other properties are exactly

For example

let person = { id: 1, someAttribute: 'somevalue' } // In this object, only id is certain attribute.

Other attributes like name, age, city, address along with some unknown attributes are dynamic and their types are also unknown

In this case to declare a type, we can use Index Signatures

interface Person {
  id: number;
  [key: string]: any;
}

Now you can assign any object to person variable with id as mandatory attribute

But, there is another better way which is Record<Keys,Type> utility type

So, we can write above interface as

interface Person extends Record<string, any> {
  id: number
}

The end result is same, we can assign any object to person variable with id as mandatory attribute

Example: https://bit.ly/3C9ecD9 (edited)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment