This works for me

function addProperty(object, property) {
  // add the property to the object with a value of null
  // return the object
  // note: the property name is NOT 'property'.  The name is the value 
  // object.property = property;
  object[property] = null;
  return object;
}

var obj = {x:1,y:null};
// undefined
obj
// {x: 1, y: null}
addProperty(obj, 'z');
// {x: 1, y: null, z: null}
Answer from Niccaman on Stack Overflow
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Working_with_objects
Working with objects - JavaScript - MDN Web Docs - Mozilla
February 21, 2026 - The object being inherited from ... chain for more information. You can add a property to all objects created through a certain constructor using the prototype property....
Discussions

How to add a property to an object that doesn't exist in its type?
In case anyone is still wondering return {...apiResponse, D: 123}; More on reddit.com
🌐 r/typescript
6
5
September 28, 2022
Is it possible to add dynamically named properties to JavaScript object? - Stack Overflow
Sign up to request clarification or add additional context in comments. ... @thedz: data.PropertyD needs to know the property name, which isn't dynamic enough. 2009-07-26T09:54:05.993Z+00:00 ... +1 because this helped me. But I don't understand why an object properties is handled like an array. 2013-03-05T11:56:57.683Z+00:00 ... @Bondye: That's part of the strange design of javascript... More on stackoverflow.com
🌐 stackoverflow.com
Can't add properties to javascript object
trying to do a very simple operation in some javascript code. trying to add some properties but they are not getting added. I log object before and after and they are not getting added. I have verified that the variable containing the object to be altered is of type “object” and the new ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
February 27, 2022
The shortest way to conditionally insert properties into an object literal
I don't really like using tricks like this in my code unless I'm the only person who will be reading it. Very few people would understand what the code is doing unless they're already familiar with the trick. I'd rather just add the extra 2-3 lines of code and avoid the risk of confusing people. I'm primarily a JS developer but I write/read a good amount of Perl code at work from time to time. Tricks like this seem like the standard for Perl developers and it can make it very hard to parse through Perl code when you're not already an expert. I try to avoid the same patterns in my JS. More on reddit.com
🌐 r/javascript
86
243
April 16, 2021
🌐
Scaler
scaler.com › home › topics › how to add property to an object in javascript?
How to Add Property to an object in JavaScript? - Scaler Topics
May 4, 2023 - Some of the ways to add property to object JavaScript include: using dot notation, using bracket [ ] notation, using defineProperty() method, using spread operator, and using Object.assign() method.
🌐
W3Schools
w3schools.com › js › js_object_properties.asp
JavaScript Object Properties
Object properties are key:value pairs · Access properties with dot notation or bracket notation · Add, change, and delete properties using assignment and delete · Use the in operator to check if a property exists · What are JavaScript Objects? What are Object Methods?
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-add-and-remove-properties-from-objects-in-javascript
How to add and remove properties from objects in JavaScript ? - GeeksforGeeks
August 5, 2025 - We then use both dot notation and bracket notation to add properties (city, email, hobby, and gender) to the object, Next, we use both dot notation and bracket notation to remove properties (age, occupation, hobby, and gender) from the object
🌐
CoreUI
coreui.io › blog › how-to-conditionally-add-a-property-to-an-object-in-javascript
How to Conditionally Add a Property to an Object in JavaScript · CoreUI
January 13, 2025 - JavaScript provides various ways to create objects, such as using object literals, constructors, or factory functions. The most common and concise method is the object literal, which allows you to define a simple object like this: ... However, if you need to conditionally add properties to an object, you’ll need to combine these methods with conditional logic.
Find elsewhere
🌐
Educative
educative.io › answers › how-to-add-property-to-an-object-in-javascript
How to Add Property to an object in JavaScript
We can also use the Object.defineProperty function: ... We can have more control over how the property will act with this method. In addition to setting the value with the value property, we can also set it to be writable with the writable property, and enumerable with the enumerable property.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › defineProperty
Object.defineProperty() - JavaScript - MDN Web Docs
July 20, 2025 - In order to ensure these defaults are preserved, you might freeze existing objects in the descriptor object's prototype chain upfront, specify all options explicitly, or create a null-prototype object. ... const obj = {}; // 1. Using a null prototype: no inherited properties const descriptor = Object.create(null); descriptor.value = "static"; // not enumerable, not configurable, not writable as defaults Object.defineProperty(obj, "key", descriptor); // 2.
🌐
Reddit
reddit.com › r/typescript › how to add a property to an object that doesn't exist in its type?
r/typescript on Reddit: How to add a property to an object that doesn't exist in its type?
September 28, 2022 -

EDIT: I just realized I could just deep clone the object and alter that one instead.


Heres a simplified example of what I'm trying to do:

const foo = (apiReponse: {A: number, B: number) => {
    return apiReponse.D = 123
}

Basically I am pass in an api response and using the type thats generated from codegen (apollo graphql). However I want to add a new field to it dynamically, however its throwing an error for the obvious reason.

How do I get around this or this just one big anti pattern?

Top answer
1 of 5
12
In case anyone is still wondering return {...apiResponse, D: 123};
2 of 5
1
Yes and no. Here's a little more explanation on what is going on. A JavaScript object is just a key-value dictionary/map where you can always add and remove values. That's great, but it allows developers to code in a style that is hard to debug. TypeScript to the rescue! Declaring a type makes sure that those objects only have the keys and values with the expected types, which is a lot easier to debug. Quick Answer: Please try to respect the TypeScript type-system. Maybe there's a different way to structure your code so you don't need to add random properties to objects. The real answer: TypeScrip _is_ JavaScript. You can always sneak a new property on an object, and unpack it later, it's totally fine as long as you keep the rest of the code unaware of this. TypeScript allows you to "force" the complier to skip type checks; you are basically telling it: "trust me bro". Doing a shallow-copy with the spread operator is nice, but note this is NOT adding the property to the previously existing object (which is probably for the best), but if you want to do it just like in JavaScript, then go ahead and use JavaScript: const foo = (apiReponse: {A: number, B: number}) => { (apiResponse as any).D = 123; // this property will be hidden and have no type return apiReponse; } To read the sneaked-in property from outside the function, you can also skip the safety of TypeScript: const fooResponse = foo(apiResponse); // TypeScript error: property D not in {A: number, B: number} console.log(fooResponse.D); // This works, but just like in JavaScript, D could be undefined or anything else console.log((fooResponse as any).D); If you do this, be ready to defend your case when your team (or linter) says that you shoudl never use type "any".
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › assign
Object.assign() - JavaScript - MDN Web Docs
The Object.assign() static method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.
🌐
SheCodes
shecodes.io › athena › 513-how-to-add-property-to-an-object-in-javascript
[JavaScript] - How to Add Property to an Object in | SheCodes
Learn how to add a property to an object using dot notation and square bracket notation in JavaScript
🌐
Kevinpeters
kevinpeters.net › adding-object-properties-conditionally-with-es-6
Adding object properties conditionally with EcmaScript 6 (ES6)
July 16, 2019 - Ever struggled with adding or deleting a property in JavaScript? Check this article out to learn how omitting or adding properties works in ES5 and in ES6.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-add-a-property-to-a-javascript-object-using-a-variable-as-the-name
Add a Property to JavaScript Object Using a Variable as the Key - GeeksforGeeks
October 14, 2025 - let object1 = { firstname: "Romy", ... newPropertyValue = 25; // Step 2: Add the new property using Object.assign() const updatedObject = Object.assign({}, object1, { [newPropertyName]: newPropertyValue }); // Step 3: Access and display ...
🌐
Medium
elvisciotti.medium.com › conditional-adding-object-properties-in-javascript-in-one-line-de97f5de449a
Conditional adding object properties in Javascript in one line | by Elvis Ciotti | Medium
April 2, 2025 - One-liner code alternative to if (condition) { object.prop = 'value';}`. In JavaScript, you can conditionally add object properties using an if statement, a ternary operator or even a single instruction using the spread operator.
🌐
DEV Community
dev.to › sanchithasr › how-to-add-modify-and-delete-javascript-object-literal-properties-49cd
Add, Update and Delete properties from JavaScript Object - DEV Community
December 24, 2020 - One can add the property to an object by simply giving it a value. Like below example, we are adding the property husband and giving the value directly.
🌐
Futurestud.io
futurestud.io › tutorials › conditionally-add-properties-to-an-object-in-javascript-or-node-js
Conditionally Add Properties to an Object in JavaScript or Node.js
December 22, 2022 - You may accept the options as an argument in a method and then determine whether to add a property. The following code snippet illustrates a createUser function that accepts and options object. The only option supported is shouldAddTimestamps. You can combine JavaScript’s spread operator and a boolean check which defines whether to add properties to the target object.
🌐
Coderwall
coderwall.com › p › m-rmpq › dynamically-creating-properties-on-objects-using-javascript
Dynamically creating properties on objects using javascript (Example)
June 26, 2023 - So now that we know how to create properties on javascript objects, let us look at how to create dynamic properties on Javascript ... This will output the value some_value on the console. Defining a dynamic property using Object.defineProperty. The documentation for Object.defineProperty ... // Example of an object property added with defineProperty with a data property descriptor Object.defineProperty(obj, "property3", {value : 'some value', writable : true, enumerable : true, configurable : true}); // 'property3' property exists on object obj and its value is 37