2

This is what I want

type Report = {
  branches: number; // should be required
  functions: number; // should be required
  lines: number; // should be required
  statements: number; // should be required
};

const report: Report = {
  branches: 10,
  functions: 19,
  lines: 54,
  statements: 67,
}

but I can’t do this

const items = Object.keys(report).map(key => report[key])

typescript (version 4.3.5) shows the following error on report[key]:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Report'. No index signature with a parameter of type 'string' was found on type 'Report'.ts(7053)

So I try this

export type Report = {
  [key: string]: number;
  branches: number;
  functions: number;
  lines: number;
  statements: number;
};

but now it allows something like

const withFoo: Report = {
  branches: 25.3,
  imABug: 45, // should not be allowed!
  functions: 20.1,
  lines: 70.01, 
  statements: 45,
},
Kirk Strobeck
  • 17,984
  • 20
  • 75
  • 114
  • What do you mean by "cant map it"? – Dharmaraj Sep 01 '21 at 13:48
  • ^ I revised the question – Kirk Strobeck Sep 01 '21 at 13:49
  • ^ I revised the question – Kirk Strobeck Sep 01 '21 at 13:53
  • What do you mean with "I can't `Object.keys(foo).map()` it"? [This works](https://www.typescriptlang.org/play?#code/C4TwDgpgBAShYHsBOwoF4oG8BQUoCMkBDAOwGMALCAZwC4oSBXAW3wiQG4oB6bqaigkYAbACYFoSCAEdGASymjcUAGaNywOQhJ0GLNpx58BQsRKhTZCiErzC5JGvSat2XXv0EjxbCzPmKytTARMAQzBAkwLouBu7GXma+lgE22AC+HNjYZNrBqggI9HCIKOhYyoSklE5QAIwADAA0ymoaWjr0dQCcLXYOtQCsACx9-CFhEVG6AGwA7C3p2QDy+ABWEGTAAHQA1hAg1AAUKoUAlNvMRGBHEMLoAHxQuToIwhDbwggA5rfCZ2cgA)? – A_A Sep 01 '21 at 13:55
  • 1
    Seems to work just fine in the [Typescript playground](https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBDAnmYcBKxKzgXjgbwCg44AjKAQwDsBjAC2AGcAuOKgVwFtTgoBuOAHpBcRnQjsANgBMyqKMACO7AJYLpxOADN2tGCohUWbLj35CRYiTLlwFytcA0lJKqk1YduvAcNHipWR47JVV1TUYYChhgTmAqGGMvM19LAJtg+zCnQgBfPkJCGkNIuAB3FRg6ADEICFYMLHg8IhJyanoPOAAmAFYAOgBmABpNHT0DI1ZugAZ+gEZRlzcugHY5mcW4CKiYuITjABZe0dzC4qN4UgooXDgAeVIAK2AaGH6Aa2BERgAKCqqtQgAEp+pwKGBfpVYrgAHxwAAG4LAKFkABJ8NDOLkEcCCkA). – Etheryte Sep 01 '21 at 13:55
  • @Etheryte yes. I'm not sure if OP is referring to "No index signature with a parameter of type 'string' was found on type 'Report'" when trying to read value `foo[item]` – Dharmaraj Sep 01 '21 at 13:57
  • ...in which case, `Object.entries(foo).map(([key,value])=>...)` would sidestep the type mismatch – spender Sep 01 '21 at 13:59
  • ^ I’ll add the error report to the question – Kirk Strobeck Sep 01 '21 at 14:12
  • 1
    The error report of what code? Afaik, the code you show us won't produce this error. Without knowing the code that produces it, and knowing what you want to achieve it's hard to give suggestions – A_A Sep 01 '21 at 14:19
  • typescript version 4.3.5 – strict – Kirk Strobeck Sep 01 '21 at 14:20
  • This line: `Object.keys(foo).map()` won't produce the error. Likely what is inside of `map(...)`. Can you show that and tell us what you want to achieve with this line of code? – A_A Sep 01 '21 at 14:21
  • ^ Updated question per feedback – Kirk Strobeck Sep 01 '21 at 14:25

1 Answers1

2

That's because you are creating an array of strings with Object.keys() and trying to map to Report object, which I feel is just bad implementation.

Why don't you try this for iterating over the properties instead:

type Report = {
  branches: number; // should be required
  functions: number; // should be required
  lines: number; // should be required
  statements: number; // should be required
};

const foo: Report = {
  branches: 10,
  functions: 19,
  lines: 54,
  statements: 67,
}


Object.entries(foo).map(([key, value]) => console.log(key, value));
Salvino D'sa
  • 4,018
  • 1
  • 7
  • 19
  • Note that you could [get more keys](https://www.typescriptlang.org/play?ssl=22&ssc=50&pln=1&pc=1#code/C4TwDgpgBAShYHsBOwoF4oG8BQUoCMkBDAOwGMALCAZwC4oSBXAW3wiQG4oB6bqaigkYAbACYFoSCAEdGASymjcUAGaNywOQhJ0GLNpx58BQsRKhTZCiErzC5JGvSat2XXv0EjxbCzPmKytTARMAQzBAkwLouBu7GXma+lgE22AC+HNjYoJBQAMqMkEhwiCjosPDIqABkWEEIEQByEADuAApICMWg9ABMAMwALBnZZNrBqggI9IXFpdUVOHiEpJROUACMAAwANMpqGlo69JsAnPt2DhsArEOX-CFhEVG6AGwA7A-UjRAtHV0eiB+sNRthxjpUPgiEh6AtyhgVNNsgB5fAAKwgZGAADpIsAkHIaAAKaFIACUOOYRDAxOJEGE5PQAD4oBCfsIIDjhAgAOb0xnkjhAA) than present in the original type – A_A Sep 01 '21 at 14:35
  • Interesting – https://caniuse.com/object-entries is a bit worse than https://caniuse.com/?search=object.keys but not by much – I wonder if my setup would auto-polyfill anyway – Kirk Strobeck Sep 01 '21 at 18:28