'How to generate N-length tuple of same item in typescript for function arguments
Is there a way to quickly pass multiple null (or any other) arguments to function call in typescript. Given example below I need only first argument and others require to be some type or null. Is there any way to generate typescript type for N-length of tuple so I can create a helper function which will return that type tuple. Something like below.
const functionToCall = (arg0: string | null, ..., arg50: string | null) => {
// ....
}
const getNLengthOfValue = (value: any, repetitions: number) => {
return [...new Array(repetitions)].map(() => value);
}
functionToCall("some value", ...getNLengthOfValue(null, 50));
P. S. I know I can use rest param, but my question is not about API, this is just a rough example. I want to know how to create type for N length tuple of same type items and how to return it from function.
Solution 1:[1]
You can use an rest parameter like this:
const functionToCall = (arg0: string | null, ...restArgs: (string | null)[]) => {
// ...
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MrCodingB |
