703

Trying out TypeScript for a React project and I'm stuck on this error:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ train_1: boolean; train_2: boolean; train_3: boolean; train_4: boolean; }'.
  No index signature with a parameter of type 'string' was found on type '{ train_1: boolean; train_2: boolean; train_3: boolean; train_4: boolean; }'

Which appears when I try to filter the array in my component

.filter(({ name }) => plotOptions[name]);

So far I looked at the article "Indexing objects in TypeScript" (https://dev.to/kingdaro/indexing-objects-in-typescript-1cgi) since it had a similar error, but I tried to add the index signature to type plotTypes and I still get the same error.

My component code:

import React, { Component } from "react";
import createPlotlyComponent from "react-plotly.js/factory";
import Plotly from "plotly.js-basic-dist";
const Plot = createPlotlyComponent(Plotly);

interface IProps {
  data: any;
}

interface IState {
  [key: string]: plotTypes;
  plotOptions: plotTypes;
}

type plotTypes = {
  [key: string]: boolean;
  train_1: boolean;
  train_2: boolean;
  train_3: boolean;
  train_4: boolean;
};

interface trainInfo {
  name: string;
  x: Array<number>;
  y: Array<number>;
  type: string;
  mode: string;
}

class FiltrationPlots extends Component<IProps, IState> {
  readonly state = {
    plotOptions: {
      train_1: true,
      train_2: true,
      train_3: true,
      train_4: true
    }
  };
  render() {
    const { data } = this.props;
    const { plotOptions } = this.state;

    if (data.filtrationData) {
      const plotData: Array<trainInfo> = [
        {
          name: "train_1",
          x: data.filtrationData.map((i: any) => i["1-CumVol"]),
          y: data.filtrationData.map((i: any) => i["1-PressureA"]),
          type: "scatter",
          mode: "lines"
        },
        {
          name: "train_2",
          x: data.filtrationData.map((i: any) => i["2-CumVol"]),
          y: data.filtrationData.map((i: any) => i["2-PressureA"]),
          type: "scatter",
          mode: "lines"
        },
        {
          name: "train_3",
          x: data.filtrationData.map((i: any) => i["3-CumVol"]),
          y: data.filtrationData.map((i: any) => i["3-PressureA"]),
          type: "scatter",
          mode: "lines"
        },
        {
          name: "train_4",
          x: data.filtrationData.map((i: any) => i["4-CumVol"]),
          y: data.filtrationData.map((i: any) => i["4-PressureA"]),
          type: "scatter",
          mode: "lines"
        }
      ].filter(({ name }) => plotOptions[name]);
      return (
        <Plot
          data={plotData}
          layout={{ width: 1000, height: 1000, title: "A Fancy Plot" }}
        />
      );
    } else {
      return <h1>No Data Loaded</h1>;
    }
  }
}

export default FiltrationPlots;

20 Answers 20

915

For anyone who stumbles upon this in the future:

If you're getting the TypeScript error

'...expression of type string cannot be used to index...'

then simply specify that the 'expression of type string' is a key of the type of that object. For example,

const someObj:ObjectType = data;
const field = 'username';

// This gives an error
const temp = someObj[field];

// Solution 1: When the type of the object is known
const temp = someObj[field as keyof ObjectType]

// Solution 2: When the type of the object is not known
const temp = someObj[field as keyof typeof someObj]
8
  • 39
    this show "Type 'any' is not assignable to type 'never" error Dec 11, 2021 at 10:34
  • 1
    @VikramDeshmukh thanks for the edit, but I deleted my comment. There was a large amount of edge cases I wasn't factoring in. See this huge discussion about it: github.com/microsoft/TypeScript/issues/21732 As far as I can digest, its not yet a solved problem
    – Chris
    Jul 5, 2022 at 15:11
  • 2
    If you index an object T with a key that is not in this object, the resulting value is undefined. The type you want is T[K] | undefined, not just T[K]. Jul 13, 2022 at 11:59
  • 9
    Or like const myObject: Record<string, any> = {}
    – SeanMC
    Sep 15, 2022 at 21:36
  • 7
    Is there a less verbose solution? because this really hurt readability
    – TOPKAT
    Dec 19, 2022 at 19:01
381

This happens because you try to access plotOptions property using string name. TypeScript understands that name may have any value, not only property name from plotOptions. So TypeScript requires to add index signature to plotOptions, so it knows that you can use any property name in plotOptions. But I suggest to change type of name, so it can only be one of plotOptions properties.

interface trainInfo {
    name: keyof typeof plotOptions;
    x: Array<number>;
    y: Array<number>;
    type: string;
    mode: string;
}

Now you'll be able to use only property names that exist in plotOptions.

You also have to slightly change your code.

First assign array to some temp variable, so TS knows array type:

const plotDataTemp: Array<trainInfo> = [
    {
      name: "train_1",
      x: data.filtrationData.map((i: any) => i["1-CumVol"]),
      y: data.filtrationData.map((i: any) => i["1-PressureA"]),
      type: "scatter",
      mode: "lines"
    },
    // ...
}

Then filter:

const plotData = plotDataTemp.filter(({ name }) => plotOptions[name]);

If you're getting data from API and have no way to type check props at compile time the only way is to add index signature to your plotOptions:

type tplotOptions = {
    [key: string]: boolean
}

const plotOptions: tplotOptions = {
    train_1: true,
    train_2: true,
    train_3: true,
    train_4: true
}
2
  • 11
    {[key: string]: boolean} this helped a lot, very intiutive
    – kakabali
    May 24, 2022 at 17:23
  • 4
    You can also skip the whole type/interface and declare const plotOptions:{[key:string]:boolean} = {....}
    – Mikepote
    Aug 29, 2022 at 23:20
136

When using Object.keys, the following works:

Object.keys(this)
    .forEach(key => {
      console.log(this[key as keyof MyClass]);
    });
3
  • 12
    Excellent! Why does this solve the issue?
    – Andru
    Jan 29, 2021 at 8:31
  • @Andru this solves the issue because TS recognizes that the key is something that this actually has. In fact, we can even get rid of the class name and use this instead like this[key as keyof this]! Mar 27, 2021 at 1:41
  • 1
    @ChristosLytras I see. A pity TypeScript has to be told this by a type cast. ..What else could key actually be when it's a loop over the elements of Object.keys? - In a future TypeScript version such a type cast might not be necessary, I guess.
    – Andru
    Apr 10, 2021 at 16:49
99

I use this:

interface IObjectKeys {
  [key: string]: string | number;
}

interface IDevice extends IObjectKeys {
  id: number;
  room_id: number;
  name: string;
  type: string;
  description: string;
}

NOTE: "[key: string]" what is it? An object in JavaScript is primarily just a collection of properties made up of key-value pairs. Moreover, the key can only be a string (even for array elements), but the value can be any data type.

If you use the optional property in your object:

interface IDevice extends IObjectKeys {
  id: number;
  room_id?: number;
  name?: string;
  type?: string;
  description?: string;
}

... you should add 'undefined' value into the IObjectKeys interface:

interface IObjectKeys {
  [key: string]: string | number | undefined;
}
9
  • 1
    Nope ?optional that means maybe undefined so should I add it like this [key: string]: string | number | undefined. that solves the issue Property 'room_id' of type 'number | undefined' is not assignable to string index type 'string | number'. :)
    – Oliver D
    Feb 2, 2021 at 8:56
  • 1
    @GennadyMagomaev Hello, I had the same problem and your answer helped me out a lot! I dont fully understand what this means though : "[key: string]: string | number". Does it mean that the key of the object can be a string and after the " :" does it mean it can also be a string or number or ? I got confused... Could you be kind enough to explain it to me please ? Mar 7, 2021 at 16:49
  • 3
    Best, cleanest, and most flexible solution.
    – AMS777
    Jun 2, 2021 at 9:20
  • 1
    You can also just extend Record<string, any>, Replace any with options if you want.
    – user2157850
    Nov 5, 2021 at 14:55
  • 1
    Thanks, in my case, only add an index in my type [key: string] was enough. Apr 10 at 14:48
82
// bad
const _getKeyValue = (key: string) => (obj: object) => obj[key];

// better
const _getKeyValue_ = (key: string) => (obj: Record<string, any>) => obj[key];

// best
const getKeyValue = <T extends object, U extends keyof T>(key: U) => (obj: T) =>
  obj[key];

Bad - the reason for the error is the object type is just an empty object by default. Therefore it isn't possible to use a string type to index {}.

Better - the reason the error disappears is because now we are telling the compiler the obj argument will be a collection of string/value (string/any) pairs. However, we are using the any type, so we can do better.

Best - T extends empty object. U extends the keys of T. Therefore U will always exist on T, therefore it can be used as a look up value.

Here is a full example:

I have switched the order of the generics (U extends keyof T now comes before T extends object) to highlight that order of generics is not important and you should select an order that makes the most sense for your function.

const getKeyValue = <U extends keyof T, T extends object>(key: U) => (obj: T) =>
  obj[key];

interface User {
  name: string;
  age: number;
}

const user: User = {
  name: "John Smith",
  age: 20
};

const getUserName = getKeyValue<keyof User, User>("name")(user);

// => 'John Smith'

Alternative Syntax

const getKeyValue = <T, K extends keyof T>(obj: T, key: K): T[K] => obj[key];
2
  • 4
    I wrote a tiny npm package with this function to make this task easier for those that are new to Typescript.
    – Alex Mckay
    Jun 30, 2020 at 0:58
  • The package is approximately 38 bytes once packaged and minified.
    – Alex Mckay
    Jun 30, 2020 at 1:14
63

I fixed this issue by using keyof

messageMap = {
 "Hi": "Hello",
 "Who are you": "My name is Test Sat Bot",
 "What is your role": "Just guide for the user",
}

this ❌

let answer = this.messageMap[question];

replace with ✔️

let answer = this.messageMap[question as keyof typeof this.messageMap];

Here question is type of string

getBotMessage(question: string){    
  let answer = this.messageMap[question as keyof typeof this.messageMap];
}
2
  • 1
    another reason why TS solves less problems than it creates. You go from 39 chars of valid JS code that works 100% of the time to 71 chars of TS that just compiled down to the original 39 chars. so dumb.
    – LMS5400
    Aug 30 at 20:33
  • Yes I had to replace it like 300x in my code: Before: strings.names[nameId] After: strings.names[nameId.toString() as keyof typeof strings.names])
    – Rebs
    Sep 4 at 11:03
28

I have made a simulation of the problem. looks like the issue is how we should Access Object Properties Dynamically Using Bracket Notation in Typescript

interface IUserProps {
  name: string;
  age: number;
}

export default class User {
  constructor(private data: IUserProps) {}

  get(propName: string): string | number {
    return this.data[propName as keyof IUserProps];
  }
}

I found a blog that might be helpful to understand this better.

here is a link https://www.nadershamma.dev/blog/2019/how-to-access-object-properties-dynamically-using-bracket-notation-in-typescript/

1
  • Sorry for that @double-beep, I have UPDATED my post. It was actually my first post to answer a question here. Jan 28, 2021 at 12:20
13

When we do something like this obj[key] Typescript can't know for sure if that key exists in that object. What I did:

Object.entries(data).forEach(item => {
    formData.append(item[0], item[1]);
});
10

This might help someone First the empty object need to be with the type Record<string, any> so it will accept key as string and value as any

const sortFields: Record<string, any> = {};
const keyName = `field`;

sortFields[keyName] = 'asc';

So here when we log sortFields

 console.log("sortFields==== ", sortFields); // sortFields==== {field: 'asc'}

Or if we want to add more than one element in the object we can do it like this

sortFields.email = "asc"; // Or sortFields["email"] = "asc"; but its better written in dot notation.
sortFields.name = "desc";
sortFields.phone = "desc";

So now when we log sortFields

 console.log("sortFields==== ", sortFields); // sortFields==== {field: 'asc', email: 'asc', name: 'desc', phone: 'desc'}
8

With out typescript error

    const formData = new FormData();
    Object.keys(newCategory).forEach((k,i)=>{  
        var d =Object.values(newCategory)[i];
        formData.append(k,d) 
    })
1
  • 3
    works fine, but i suggest to use forEach instead of map, cause a map usually returns something.
    – Cobi
    Jun 8, 2021 at 10:56
7

Thanks to Alex Mckay I had a resolve for dynamic setting a props:

  for(let prop in filter)
      (state.filter as Record<string, any>)[prop] = filter[prop];
6

It worked for me with keyof and as operators:

const keys: [keyof ITrainInfo] = Object.keys(this.trainInfo) as [
    keyof ITrainInfo,
]
keys.forEach((property) => {
    // console.log(tmpUser[property])
    if (this.trainInfo === undefined) return
    if (this.trainInfo[property] !== undefined) {
        // your code here
        /*const trainsToSet = trains.find((field) => field.name === property)
        if (trainsToSet != undefined)
            trainsToSet.value = this.trainInfo[property]?.toString()
        */
    }
})
6

as a last resort, you can mute this error by setting "suppressImplicitAnyIndexErrors": true in tsconfig.json

{
  "compilerOptions": {
    "suppressImplicitAnyIndexErrors": true,
  }
}
4

I made some small changes to Alex McKay's function/usage that I think make it a little easier to follow why it works and also adheres to the no-use-before-define rule.

First, define this function to use:

const getKeyValue = function<T extends object, U extends keyof T> (obj: T, key: U) { return obj[key] }

In the way I've written it, the generic for the function lists the object first, then the property on the object second (these can occur in any order, but if you specify U extends key of T before T extends object you break the no-use-before-define rule, and also it just makes sense to have the object first and its' property second. Finally, I've used the more common function syntax instead of the arrow operators (=>).

Anyways, with those modifications you can just use it like this:

interface User {
  name: string;
  age: number;
}

const user: User = {
  name: "John Smith",
  age: 20
};

getKeyValue(user, "name")

Which, again, I find to be a bit more readable.

0
1

TypeScript needs to be sure those values exist in trainInfo, else it reads all as string

instead of doing this

interface trainInfo {
  name: string;
  x: Array<number>;
  y: Array<number>;
  type: string;
  mode: string;
}

do this

interface trainInfo {
  name: "train_1" | "train_2" | "train_3"| "train_4";
  x: Array<number>;
  y: Array<number>;
  type: string;
  mode: string;
}
1

In situation of nested object, if want for in in for in , here is a working example without error warning:

export const X = {
  aa: {
    a: '1',
    b: '2',
  },
  bb: {
    a: '3',
    b: '4',
  },
} as const

export const f1 = () => {
  let k1: keyof typeof X
  for (k1 in X) {
    console.log(k1)

    let k2: keyof (typeof X)[keyof typeof X]
    for (k2 in X[k1]) {
      console.log(X[k1][k2])
    }
  }
}
0

This is not a answer to the original question, but a generic work around to this problem.

Original problem: person[cr.field] causes this error


I'm doing a generic advanced search form where user can select a field, comparator and the desired value. When trying to read the value from the object based on the key, I get this error (altought the field value is type of string and I think it should be just fine)

So what I do is I extract the [key, value] like this

const x: [string, any] = Object.entries(person).find(([key, _]) => key === cr.field);

For example if my criterion (cr) is { field: 'name', value: 'John' } and field name actually exists in a person obj., it should return the field name and the value as tuple (x is [string, any] or undef). If not found, undefined.

0

public getUserName(): string {

const accessToken = this.getAccessToken();
const claims:any = this.getUserClaims();
console.log('access token ',accessToken);
this.getUserInfo();
return claims['sub'].split('@')[0];

}

//give any type to the variable

-1

I know it's a little too late, but all it's needed is to add a little type conversion, I wrote a static function that safely-returns the array of keys with the correct typing. All you need is to define the type and pass the object as a parameter:

export class ObjectUtil {
  public static getObjectKeys<T>(obj: Object) {
    if (!obj) {
      return [];
    }

    return Object.keys(obj).map((key: string) => key as keyof T);
  }
}

Below is a simple example:

ObjectUtil.getObjectKeys<Address>(address).forEach((key) => {
  console.log(address[key]);
});
-28

This is what it worked for me. The tsconfig.json has an option noImplicitAny that it was set to true, I just simply set it to false and now I can access properties in objects using strings.

8
  • 18
    This will remove strict, then there is no point in using typescript if we keep removing these restrictions.
    – Rip3rs
    Mar 12, 2020 at 12:13
  • 34
    This does not solve the problem, it just ignores it. Mar 17, 2020 at 21:27
  • 2
    @Zeke I understand mate :) I was in a rush writing. What I meant is that if we keep solving the issues by just telling it to ignore then there is no point of it in the first place. but then again all depends on the project, and the decisions per project.
    – Rip3rs
    Mar 24, 2020 at 11:41
  • 2
    I prefer this... even strong type language like c# have "var". Refering stackoverflow.com/questions/62377614/… ... A bit over engineered to put all sort of bumping code to access a simple property.
    – Eric
    Jun 22, 2020 at 5:27
  • 1
    var in C# is not like any in Typescript. var will not work in a strongly typed language like C# if the type cannot be inferred, whereas any allows a variable to take on any type at any time - an any variable can be reassigned to a value of a completely different type. This is not possible in C#. Jun 25, 2020 at 15:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.