C
C
cocomuffin2019-03-21 12:10:38
typescript
cocomuffin, 2019-03-21 12:10:38

How to type the rest-operator when destructuring?

Good day!
Please tell me how to write the typing correctly in this example:

type snbnus = string | number | boolean | null | undefined | symbol;

const func = (...elements: snbnus[]) => {
    const [targetArray, ...rest] = elements;
};

At the same time, in elements, the first element will necessarily be an array (the elements of which can be of any type from the snbnus alias), and the rest-operator, respectively, can simply be of any type from the snbnus alias. Recording
const [targetArray: snbnus[], ...rest: snbnus[]] = elements;

highlighted as an error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
artem_ibragimov, 2019-03-21
@artem_ibragimov

i would do that

type snbnus = string | number | boolean | null | undefined | symbol;

const func = (targetArray: snbnus[], ...rest: snbnus[]) => {
    console.log(targetArray, rest);
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question