Closed
Description
TypeScript Version: 2.4.2
Code
interface HasIndex {
[key : string] : number
}
interface HasPropWithHasIndex {
hasIndex ?: HasIndex
}
// one - works fine
const obj1 : HasPropWithHasIndex = {
hasIndex: {
a: 1
}
}
// two - works fine
const test2 = { a: 1 }
const obj2 : HasPropWithHasIndex = {
hasIndex: test2
}
// three - doesn't work
const obj = {
a: 1,
b: 2,
c: 3,
}
const { a, ...test3 } = obj
// Error: Index signature is missing in type '{ b: number; c: number; }'.
const obj3 : HasPropWithHasIndex = {
hasIndex: test3,
}
// four - test4 should be of type number, is instead of type any
const test4 = test3['a']
Expected behavior:
The obj3
assignment in test three should work without an issue.
test4
in test four should be of type number
Actual behavior:
The obj3
assignment in test three throws an error: Index signature is missing in type '{ b: number; c: number; }'.
test4
is of type any
.
It looks like when you use the spread operator, typescript changes the type of the index operator to return any
.