0

I am not assigning new property like this:Typescript error: TS7053 Element implicitly has an 'any' type Hence any would not work. I am just trying to edit an existing proprety based on an interface

   updateFieldTest(index: number, field: string ) {
        const tasks: Foo[] = [
            {foo:1,bar:2},
            {foo:1,bar:2},
            {foo:1,bar:2},
            {foo:1,bar:2},
        ]
        const task = tasks[index];
        task[field] = 4
    }

The interface Foo

export  interface Foo {
    foo: number,
    bar: number
}

Gives this error

ERROR in src/app/app.component.ts(391,9): error TS7017: Element implicitly has an 'any' type because type 'Foo' has no index signature

As temporary solution, changed field: string to field: 'foo' | 'bar'

But is there a better way?

TSR
  • 17,242
  • 27
  • 93
  • 197
  • Possible duplicate of [Typescript error: TS7053 Element implicitly has an 'any' type](https://stackoverflow.com/questions/56833469/typescript-error-ts7053-element-implicitly-has-an-any-type) – Murat Karagöz Jul 01 '19 at 10:01
  • @MuratKaragöz This is not a duplicate. I am not assigning new property like this:Typescript error: TS7053 Element implicitly has an 'any' type Hence any would not work. I am just trying to edit an existing proprety based on an interface Please see edit. – TSR Jul 01 '19 at 10:06

1 Answers1

0

You are missing the index type for Foo which is string derived from field:string.

interface Foo {
    foo: number,
    bar: number,
    [index: string]: any
}
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107