I want MyInterface.dic
to be like a dictionary name: value
, I define it as follows:
interface MyInterface {
dic: { [name: string]: number }
}
Now I create a function which waits for my type:
function foo(a: MyInterface) {
...
}
And the input:
let o = {
dic: {
'a': 3,
'b': 5
}
}
I'm expecting foo(o)
to be correct, but the compiler is falling:
foo(o) // Typescript error: Index signature is missing in type { 'a': number, 'b': number }
I know there is a possible casting: let o: MyInterface = { ... }
which do the trick but the question is, why typescript is not recognizing my type?
Extra: works fine if o
is declared inline:
foo({
dic: {
'a': 3,
'b': 5
}
})
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…