"OK" is a string, and str is implicitly taking the type string in your code.

When you try to access an object's property, you need to use a type keyof. TypeScript then knows you are not assigning a random string; you are assigning strings compatible with the properties (keys) for the object.

Also, since status is a variable, not a type, you need to extract its type with typeof.

Try:

let str = "OK" as keyof typeof status;
status[str]; // 200

or more cleanly:

type StatusKey = keyof typeof status;
let str: StatusKey = "OK";
status[str]; // 200

// and to answer the question about reversal
status[status.OK as StatusKey]; // OK

See: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#keyof-and-lookup-types

Answer from Guillaume F. on Stack Overflow
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-access-object-property-dynamically
Dynamically access an Object's Property in TypeScript | bobbyhadz
Use keyof typeof obj as the type of the dynamic key. Use bracket notation to access the object's property, e.g.
Discussions

reactjs - Dynamically accessing object property in TypeScript - Stack Overflow
However I am getting the error that either a string cannot be used as an index type, having the type be "any" doesn't help either. I know this is possible but my TypeScript skills are lacking. How can I dynamically access an object's property in TypeScript? More on stackoverflow.com
🌐 stackoverflow.com
Object dynamic property acces in TypeScript - Stack Overflow
Sorry if this has been asked/answered previously but I am trying to find how - if possible - to properly use features of TypeScript to dynamically read from properties of an object. I have taken a look at these examples/resources: https://codewithstyle.info/Deep-property-access-in-TypeScript/ More on stackoverflow.com
🌐 stackoverflow.com
typescript - How to dynamically access to object properties? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I need to dynamically access to object property in forEach. More on stackoverflow.com
🌐 stackoverflow.com
typescript - How to type a function to dynamically access an object’s property? - Stack Overflow
I would like to make this JavaScript function type safe. It takes a property name prop and returns an accessor function. The accessor function takes an object and returns the value of the property ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
CopyProgramming
copyprogramming.com › howto › how-to-dynamically-access-object-property-in-typescript
Dynamically Accessing Object Properties in TypeScript: A Guide - Typescript
April 20, 2023 - What is the process for converting this usage to TypeScript? ... The variable "OK" is of string type, and when used in your code, str is assumed to also be a string type. To access an object of type object's property , it is necessary to use a specific keyof .
🌐
Webdevtutor
webdevtutor.net › blog › typescript-dynamically-access-object-property
A Comprehensive Guide to Dynamically Accessing Object Properties in TypeScript
When dynamically accessing object properties, TypeScript may not infer the exact type of the accessed property. To address this issue, you can use type assertions to explicitly specify the type of the accessed property. Here's how you can do it: interface Product { name: string; price: number; ...
🌐
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 - Dynamically assign properties to TypeScript objects using the Record and Partial utility types, optional object properties, and more.
🌐
Squash
squash.io › tutorial-getting-object-value-by-dynamic-key-in-typescript
How to Get an Object Value by Dynamic Keys in TypeScript
October 14, 2023 - By using the bracket notation ([]), we can dynamically access the object property and assign it to the propertyValue variable. Related Article: Tutorial: Working with Dynamic Object Keys in TypeScript...
🌐
Stack Overflow
stackoverflow.com › questions › 63490126 › object-dynamic-property-acces-in-typescript
Object dynamic property acces in TypeScript - Stack Overflow
To restate the question, "What is the proper way to access a property on an object using a variable?" ... If you are sure that keys will not have any unexpected entries (and this is not always obvious) then you can assert const keys = Reflect.ownKeys(my) as Array<keyof Person>. ... @jonrsharpe yes. That fixes my issue. Thank you. I am still fairly new to types and TypeScript.
Find elsewhere
🌐
Thoughtbot
thoughtbot.com › blog › safe-dynamic-object-access-in-typescript
Safe dynamic object access in TypeScript
July 10, 2026 - Accessing object properties, especially dynamically, is a common cause of errors in programs. That’s because there is no guarantee that those keys are present on the object. In JavaScript, that means you could be getting unexpectedly undefined ...
🌐
Webdevtutor
webdevtutor.net › blog › typescript-access-object-property-dynamically
A Comprehensive Guide to Dynamic Property Access in TypeScript
Here's an example: interface Person ... dynamically console.log(dynamicPropertyValue); // Output: Alice · Type guards can be used to ensure type safety when accessing object properties dynamically....
🌐
Webdevtutor
webdevtutor.net › blog › typescript-get-property-dynamically
Dynamic Property Access in TypeScript: Exploring Ways to Get Properties Dynamically
The Object.keys method can also be utilized to access object properties dynamically. By retrieving an array of keys, you can then access the corresponding values. Here's an example: interface Product { name: string; price: number; } const product: Product = { name: 'Laptop', price: 999 }; const ...
🌐
Webdevtutor
webdevtutor.net › blog › typescript-dynamic-get-property
Exploring Dynamic Property Access in TypeScript
In TypeScript, you can access object properties dynamically using square brackets notation. This allows you to access properties by providing the property name as a string variable. interface User { name: string; age: number; } const user: User = { name: "John Doe", age: 30 }; const propertyName ...
🌐
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 store the property name "name" in a variable property and then use the square bracket notation to access the dynamic property. To iterate over the properties of a dynamic object in TypeScript, you can use a for...in loop. ...
🌐
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 - Note that the syntax is similar to how you’d use a variable object property in standard JavaScript: const variable = "name" const obj = { [variable]: "Freecodecamp" } The TypeScript equivalent is called an object index signature.
Top answer
1 of 1
1

In order for this to work, you need to make things generic in both the type T of obj and the type K of prop, so that the type of the final result will be the indexed access type T[K].

Now if you were not writing a curried function, you could make propertyAccessor take two arguments, prop and obj, and you could constrain K to be keyof T. But that's not going to work. When you call propertyAccessor, you call it with only the prop argument of type K. And the compiler needs to infer K from that call, without knowing what you will do with the result. That is, it needs to know K before it can possibly know about T. So instead of constraining K to keyof T, you can only constrain it to some keylike type (in your case you already had string so we can use that).

So then K is essentially unconstrained except for being a string, and it returns a function which needs to be generic in T. We need to express that T[K] is a valid operation, but it's too late to say K extends keyof T. Instead we need to reverse the constraint: T extends Record<K, any>, using the Record<K, V> utility type to say that T has some property with key K.

That gives us this:

function propertyAccessor<K extends string>(prop: K):
    <T extends Record<K, any>>(obj: T) => T[K] {
    return (obj) => obj[prop]
}

Let's try it:

interface Dated {
    date: Date
}
const dated: Dated = { date: new Date() }

let goodAccess = propertyAccessor('date')(dated); // okay
// let goodAccess: Date

let badAccess = propertyAccessor('Helsinki')(dated) // error!
// ----------------------------------------> ~~~~~
// Property 'Helsinki' is missing in type 'Dated' but required in 
// type 'Record<"Helsinki", any>'.(2345)

Looks good. The correct call is accepted and the incorrect one is rejected.

Note where the error appears for badAccess. The call propertyAccessor('Helsinki') is fine by itself, as it has to be. The compiler doesn't try to anticipate that you are going to do something wrong with that function, or that you should have called it with a different argument to fix the mistake you'll make later. As far as the compiler is concerned, you are going to use that returned function to access the Helsinki property of some object. And since Dated does not have such a property, the error is reported on dated.

Playground link to code

🌐
Python Guides
pythonguides.com › dynamically-access-object-properties-typescript
Dynamically Access Object Properties By String In TypeScript
June 23, 2025 - To ensure type safety while accessing properties dynamically, TypeScript provides the keyof operator and indexed access types. The keyof operator returns a union of all the keys in an object type.
🌐
Typescript SOS
typescriptsos.com › home › faqs › how to dynamically access object property in typescript
How to dynamically access object property in typescript - Typescript SOS
September 15, 2023 - We define a variable called ... properties dynamically in Typescript can be achieved using various methods such as the dot notation, square brackets, and the “keyof” operator....
🌐
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.
🌐
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.