Index signatures

It is possible to denote obj as any, but that defeats the whole purpose of using typescript. obj = {} implies obj is an Object. Marking it as any makes no sense. To accomplish the desired consistency an interface could be defined as follows, using an index signature

interface LooseObject {
    [key: string]: any
}

var obj: LooseObject = {};

OR to make it compact:

var obj: {[k: string]: any} = {};

LooseObject can accept fields with any string as key and any type as value.

obj.prop = "value";
obj.prop2 = 88;

The real elegance of this solution is that you can include typesafe fields in the interface.

interface MyType {
    typesafeProp1?: number,
    requiredProp1: string,
    [key: string]: any
}

var obj: MyType ;
obj = { requiredProp1: "foo"}; // valid
obj = {} // error. 'requiredProp1' is missing
obj.typesafeProp1 = "bar" // error. typesafeProp1 should be a number

obj.prop = "value";
obj.prop2 = 88;

Record<Keys,Type> utility type

Update (August 2020): @transang brought up the Record<Keys,Type> utility type in comments

Record<Keys,Type> is a Utility type in typescript. It is a much cleaner alternative for key-value pairs where property-names are not known. It's worth noting that Record<Keys,Type> is a named alias to {[k: Keys]: Type} where Keys and Type are generics. IMO, this makes it worth mentioning here

For comparison,

var obj: {[k: string]: any} = {};

becomes

var obj: Record<string,any> = {}

MyType can now be defined by extending Record type

interface MyType extends Record<string,any> {
    typesafeProp1?: number,
    requiredProp1: string,
}

While this answers the Original question, the answer here by @GreeneCreations might give another perspective on how to approach the problem.

Answer from Akash on Stack Overflow
Top answer
1 of 16
1174

Index signatures

It is possible to denote obj as any, but that defeats the whole purpose of using typescript. obj = {} implies obj is an Object. Marking it as any makes no sense. To accomplish the desired consistency an interface could be defined as follows, using an index signature

interface LooseObject {
    [key: string]: any
}

var obj: LooseObject = {};

OR to make it compact:

var obj: {[k: string]: any} = {};

LooseObject can accept fields with any string as key and any type as value.

obj.prop = "value";
obj.prop2 = 88;

The real elegance of this solution is that you can include typesafe fields in the interface.

interface MyType {
    typesafeProp1?: number,
    requiredProp1: string,
    [key: string]: any
}

var obj: MyType ;
obj = { requiredProp1: "foo"}; // valid
obj = {} // error. 'requiredProp1' is missing
obj.typesafeProp1 = "bar" // error. typesafeProp1 should be a number

obj.prop = "value";
obj.prop2 = 88;

Record<Keys,Type> utility type

Update (August 2020): @transang brought up the Record<Keys,Type> utility type in comments

Record<Keys,Type> is a Utility type in typescript. It is a much cleaner alternative for key-value pairs where property-names are not known. It's worth noting that Record<Keys,Type> is a named alias to {[k: Keys]: Type} where Keys and Type are generics. IMO, this makes it worth mentioning here

For comparison,

var obj: {[k: string]: any} = {};

becomes

var obj: Record<string,any> = {}

MyType can now be defined by extending Record type

interface MyType extends Record<string,any> {
    typesafeProp1?: number,
    requiredProp1: string,
}

While this answers the Original question, the answer here by @GreeneCreations might give another perspective on how to approach the problem.

2 of 16
120

This solution is useful when your object has Specific Type. Like when obtaining the object to other source.

let user: User = new User();
(user as any).otherProperty = 'hello';
//user did not lose its type here.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ typescript โ€บ typescript add property to object
How to Dynamically Assign Properties to an Object in TypeScript | Delft Stack
March 13, 2025 - One of the most straightforward ways to dynamically assign properties to an object in TypeScript is through the use of index signatures. An index signature allows you to define the type of properties that can be added to an object without explicitly ...
๐ŸŒ
Reddit
reddit.com โ€บ r/angular2 โ€บ how to dynamically add a property to an object in typescript?
r/Angular2 on Reddit: how to dynamically add a property to an object in typescript?
August 29, 2017 -

How can I dynamically add a property to an object in typescript? Here's a sample method:

processSearchResults(responseObject)
{
    var blogPostSearchResults = Object.assign(new GetBlogPostsResponse(), responseObject);
    this.blogPostSearchResults = blogPostSearchResults.CollectionResults;

var authorList = ['John Smith', 'Bill Jones'];

//append author list to each result
for (var blogPost in this.blogPostSearchResults)
{
    blogPost.AuthorList = authorList ;
}

}

In the example above, I'm taking an object with a particular definition, and then trying to create and assign a new property dynamically to provide an additional structure for html template view binding.

๐ŸŒ
SPGuides
spguides.com โ€บ typescript-add-a-property-to-object
Add Property to TypeScript Object: 7 Practical Methods
April 21, 2025 - Check out Merge Object Arrays Without Duplicates in TypeScript ยท When you need to add properties dynamically in TypeScript (without knowing their names in advance), you can use index signatures.
๐ŸŒ
Reddit
reddit.com โ€บ r/typescript โ€บ dynamically assign object property
r/typescript on Reddit: Dynamically assign object property
July 18, 2023 -

Hi everyone, please help me with this compile error:

interface Person {
    id: string,
    name?: string,
    age?: number
}

const originalPerson: Person = {id: "123", name: "Original"}
const updatePayload: Person = {id: "123", name: "Updated"};

Object.entries(updatePayload).forEach(([key, value]) => {
    originalPerson[key as keyof Person] = value;
});

The last line is having this error:

Looks like `value` is of type `any`, and `originalPerson[key as keyof Person]` is of type `never`. I also observed that if I change the type of `age` from `number` to `string` then the error will go away.

Not sure how can I fix this issue. For context: the original object is a very big object with multiple nested levels stored in a reactive store, while the updatePayload object is much much smaller so I may not want to reassign the original object to a new one every time I want to make an update.

๐ŸŒ
DEV Community
dev.to โ€บ logrocket โ€บ how-to-dynamically-assign-properties-to-an-object-in-typescript-58fg
How to dynamically assign properties to an object in TypeScript - DEV Community
September 13, 2022 - Apart from primitives, the most ... to build an object dynamically, take advantage of the Record utility type or use the object index signature to define the allowed properties on the object....
๐ŸŒ
LogRocket
blog.logrocket.com โ€บ home โ€บ how to dynamically assign properties to an object in typescript
How to dynamically assign properties to an object in TypeScript - LogRocket Blog
October 15, 2024 - We can perform a type assertion by either using the <> brackets or the as keyword. This is particularly helpful with the dynamic property assignment because it allows the properties we want for our object to be dynamically set.
๐ŸŒ
AskJavaScript
askjavascript.com โ€บ home โ€บ how to dynamically assign properties to an object in typescript
How to Dynamically Assign Properties to an Object in TypeScript
November 3, 2023 - To dynamically assign properties to an object in TypeScript, you can use the "index notation" or the "Object.assign()" method.
Find elsewhere
๐ŸŒ
Tiloid
tiloid.com โ€บ p โ€บ dynamically-assigning-properties-to-an-object
Dynamically Assigning Properties to an Object - Tiloid
January 6, 2023 - The Object.assign() method can also be used to merge objects and add properties dynamically.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-do-i-dynamically-assign-properties-to-an-object-in-typescript
How do I dynamically assign properties to an object in TypeScript? - GeeksforGeeks
May 2, 2024 - In TypeScript, we can not assign the properties dynamically to an object directly by using the dot operator and the square brackets syntax which we generally use to assign dynamic properties in Vanilla JavaScript.
๐ŸŒ
Squash
squash.io โ€บ tutorial-working-with-dynamic-objects-in-typescript
How to Work with Dynamic Objects in TypeScript - Squash Labs
October 14, 2023 - In the above example, we create a dynamic object dynamicObject and assign properties name and age with their respective values. Since dynamicObject is of type any, TypeScript allows us to add properties dynamically.
๐ŸŒ
Web Mound
webmound.com โ€บ add-dynamic-properties-typescript
Add Any Number of Dynamic Properties to TypeScript Objects | WM
July 9, 2023 - Using the Record utility type in TypeScript is a way to make your objects more flexible. You can add properties to it dynamically, without knowing the property names in advance.
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to dynamically add properties to TypeScript objects - YouTube
[key: string]: any; in your class/interface definition will do the trick.
Published: May 22, 2020
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 45944008 โ€บ how-to-dynamically-add-a-property-to-an-object-in-typescript
how to dynamically add a property to an object in typescript? - Stack Overflow
processSearchResults(responseObject) { var blogPostSearchResults = Object.assign(new GetBlogPostsResponse(), responseObject); this.blogPostSearchResults = blogPostSearchResults.CollectionResults; var authorList = ['John Smith', 'Bill Jones']; //append author list to each result for (var blogPost in this.blogPostSearchResults) { blogPost.AuthorList = authorList ; } }
๐ŸŒ
Webdevtutor
webdevtutor.net โ€บ blog โ€บ typescript-dynamically-add-property-to-object
How to Dynamically Add Property to Object in TypeScript
The spread operator (...) can also be used to dynamically add properties to objects in TypeScript. By spreading an object into a new object, you can achieve the same result as with Object.assign().
๐ŸŒ
The Web Dev
thewebdev.info โ€บ home โ€บ how to dynamically assign properties to an object in typescript?
How to dynamically assign properties to an object in TypeScript? - The Web Dev
February 24, 2022 - And obj.typesafeProp1 = "bar" will also give us an error since 'bar' isnโ€™t a number. To dynamically assign properties to an object in TypeScript, we can define an interface that lets us accept any properties.
๐ŸŒ
TestMu AI Community
community.testmuai.com โ€บ ask a question
How can I programmatically add property to object in TypeScript? - Ask a Question - TestMu AI (formerly LambdaTest) Community
August 1, 2024 - In JavaScript, you can do this: var obj = {}; obj.prop = "value"; However, in TypeScript, this generates an error: โ€œThe property โ€˜propโ€™ does not exist on value of type '{}'โ€. How can I add new properties to an object โ€ฆ
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ bytes โ€บ how-to-add-a-property-to-an-object-in-typescript
How to Add a Property to an Object in TypeScript
September 19, 2023 - This is because TypeScript's static typing system expects objects to adhere to a specific shape. So, is it possible to add a property to an object in TypeScript? Yes! One way is to define the object with an index signature. An index signature is a way to tell TypeScript that an object could ...
๐ŸŒ
Webdevtutor
webdevtutor.net โ€บ blog โ€บ typescript-object-add-property-dynamically
How to Add Properties Dynamically to Objects in TypeScript
In this blog post, we will explore how you can achieve this in TypeScript. One way to add properties dynamically to an object in TypeScript is through the use of index signatures. Index signatures allow you to define an object with keys of a specific type that are not known at compile time.