TypeScript: How can I declare a type for a JavaScript array with different types of elements, but not with a fixed length?

Suppose I have a JavaScript array that has a certain type of different types of elements at the beginning, but then (pattern) of repeating type (s) at the end for an arbitrary number of repetitions.

How can I declare a TypeScript type that will be compatible with this JavaScript array?

interface A {
  foo: Foo,
  bar: Bar,
}

interface B {
  baz: Baz
}

interface Bat {
  // getArr(): [A, B, B, B],  // tuple type puts types at specific indexes 
                              // but only supports a fixed number of elements
  // getArr(): Array<A | B>,  // array type notation allows arbitrary number 
                              // of elements but doesn't require them to be
                              // in specific positions
}

Edit:

To clarify, I am experimenting with using TypeScript external declarations with vanilla JavaScript code to identify problems in existing JavaScript code. I understand that submission may be unreasonable; if more wisdom were used in creating the JavaScript source code, I would not mention this adventure.

+4
1

​​ TypeScript: 1

(Array<c'>) c', c' = A | B.


, , / "", .

[ A, Array<B> ]

1 , - - .

+2

All Articles