-1

In Java, we can do something like :

void myFunction(int... ints) {
   // ints will be int[], that will never be null
}

// both call method will works :
myFunction();
myFunction(0, 1, 5);

But, in angular:

myFunction(obj?: MyObj[]) {
   // obj will be an array that can be null
   // obj will by typed as "MyObj[] | undefined"
}

myFunction(); // this works
myFunction(obj1, obj2); // error

But the "obj" variable can be null. Without the ? it's not possible to run the first function.

How can I do to have an argument with unknown amount of item, but never null?

Elikill58
  • 4,050
  • 24
  • 23
  • 45
  • You can use spread operator to make any amount of parameters `myFunction(...obj: MyObj[])`. Here `obj` will be an array of arrays. that is what you need, if i understood you correctly – ZloiGoroh Oct 13 '21 at 12:25
  • I don't need array of array, just one simple array with unknow amount of item in it – Elikill58 Oct 13 '21 at 12:27
  • But what you tell seems what I was looking for. I through it will create like string[][] but no – Elikill58 Oct 13 '21 at 12:29
  • I mean, in your example `obj` is an array of `MyObj`. I gave you a solutuion to push any number of expected for this function parameters in an `obj` array in your function – ZloiGoroh Oct 13 '21 at 12:30
  • Also relevant: [JavaScript variable number of arguments to function](https://stackoverflow.com/q/2141520) | [open-ended function arguments with TypeScript](https://stackoverflow.com/q/12697275) – VLAZ Oct 13 '21 at 12:43
  • I'm not sure for your "also relevent" link because it's for basic javascript, and it's not a good idea to use it with angular – Elikill58 Oct 13 '21 at 12:46
  • @Elikill58 why do you assume there is a difference here? TS/JS share the same rest parameters syntax for variadic functions. Also: Angular is not a language. It's a framework. It doesn't have effect on language features like defining functions or methods. – VLAZ Oct 13 '21 at 12:54
  • Yes they share same things, but I'm affraid about unsupported basic JS method with typescript/angular. For example, direct action with DOM in angular project is a lot say as not good idea. – Elikill58 Oct 13 '21 at 12:56
  • Again, Angular *does not change how language features work*. Manipulating the DOM is not a language feature. Defining functions is. Operators like `+` are also part of the language. Same with other types of syntax like `[]` to define an array. Angular does not and cannot change how these things work. TypeScript *could*, since it's a separate language, but it does not since it aims to be compatible with JS. Any valid JS code should also be valid TS code. Although compiler flags might alter this if some more error prone syntax is disabled, like missing type annotations. – VLAZ Oct 13 '21 at 13:01

1 Answers1

0

You can use spread operator to make any amount of parameters myFunction(...obj: MyObj[]). Here obj:Array<MyObj[]> will be an array of your MyObj arrays.

ZloiGoroh
  • 411
  • 4
  • 11
  • This is not spread ([and it's not an operator](https://stackoverflow.com/questions/44934828/is-it-spread-syntax-or-the-spread-operator)), this is [rest parameters syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) – VLAZ Oct 13 '21 at 12:40