Dealing with a type in typescript that consists of an object where all properties must be of type number
. I am trying to create more concrete interfaces based on this type and pass them as generic parameters to a class that extends my basic type. However, I keep running into the same TypeScript error:
Index signature for type 'xxx' is missing in type 'yyy'
I wonder if there's a way to accomplish what I'm trying to do in TypeScript. The closest solution I found was extending the moreConcrete
interface from the basic
type, which eliminates the error but causes me to lose autocompletion and other intellisense features when using the interface.
You can view an example in this fiddle: Fiddle
Below is the code in the fiddle:
type basic = {
[key: string]: number
}
class A<TInput extends basic> {
}
interface moreConcrete {
a: number,
b: number
}
const test = new A<moreConcrete>(); // this does not work like this
interface otherMoreConcrete extends basic {
a: number,
b: number
}
const test2 = new A<otherMoreConcrete>(); // this does not give any errors
const typeTest: keyof otherMoreConcrete = 'as'; // this accepts as as a key of otherMoreConcrete, because of the extension to `basic`, this should be an error