Reputation: 196
Is it possible to create an interface that reflects the type generated by:
const foo = Object.assign([1,2,3], {offset: 4});
I was considering:
interface Bar {
[key: number]: number;
offset: number;
}
But I still get errors when accessing Array prototypes (map/reduce/etc).
Upvotes: 0
Views: 44
Reputation: 7428
It seems that you can have your interface Bar
to be extending Array
class:
const foo = Object.assign([1,2,3], {offset: 4});
interface Bar extends Array<Number> {
offset: number;
}
function test(a: Bar) {
console.log(a[0]);
console.log(a.length);
console.log(a.concat);
}
test(foo);
You can read more about Interfaces Extending Classes
UPDATE: In fact, you can create a separate type using intersections instead of creating a separate interface:
const foo = Object.assign([1,2,3], {offset: 4});
type Bar = number[] & {offset: number};
function test(a: Bar) {
console.log(a[0]);
console.log(a.length);
console.log(a.concat);
}
test(foo);
Upvotes: 4