Function.prototype.apply() states that the arguments parameter can be any array-like object.
Typed Arrays are array-like objects, so they should be accepted by the apply method.
Furthermore, the Math.min specification states that its arguments do not even need to be numbers:
Given zero or more arguments, this function calls ToNumber on each of the arguments and returns the smallest of the resulting values.
Knowing all of the above, what you are trying to do looks correct and it seems like a TypeScript bug.
As for why that is happening, currently the definitions for Math.min and CallableFunction.apply are as follows:
min(...values: number[]): number;
apply<T, A extends any[], R>(
this: (this: T, ...args: A) => R,
thisArg: T,
args: A
): R;
Most likely both these definitions need to be adapted to act according to the standards.
EDIT: Likely only the apply definition needs to be changed to something like:
apply<T, A extends Iterable<AT>, AT, R>(
this: (this: T, ...args: AT[]) => R,
thisArg: T,
args: A
): R;
Or to be more correct, ArrayLike<AT> should be used instead of Iterable<AT>, but for the above to work ArrayLike would need to extend from Iterable
Function.prototype.apply() states that the arguments parameter can be any array-like object.
Typed Arrays are array-like objects, so they should be accepted by the apply method.
Furthermore, the Math.min specification states that its arguments do not even need to be numbers:
Given zero or more arguments, this function calls ToNumber on each of the arguments and returns the smallest of the resulting values.
Knowing all of the above, what you are trying to do looks correct and it seems like a TypeScript bug.
As for why that is happening, currently the definitions for Math.min and CallableFunction.apply are as follows:
min(...values: number[]): number;
apply<T, A extends any[], R>(
this: (this: T, ...args: A) => R,
thisArg: T,
args: A
): R;
Most likely both these definitions need to be adapted to act according to the standards.
EDIT: Likely only the apply definition needs to be changed to something like:
apply<T, A extends Iterable<AT>, AT, R>(
this: (this: T, ...args: AT[]) => R,
thisArg: T,
args: A
): R;
Or to be more correct, ArrayLike<AT> should be used instead of Iterable<AT>, but for the above to work ArrayLike would need to extend from Iterable
The TypeScript-safe way to write this would be:
let min_value = Math.min(...arr);
Which compiles down to Math.min.apply(Math, arr);
Videos
In your component, you could add math = Math;, then in your html, change it to
<p>Showing {{(page-1) * pageSize}} to {{ math.min((page-1) * pageSize + pageSize, tasks.length)}} of {{tasks.length}}</p>.
This creates a local copy of Math without needing to create extra functions and getters
Check the original answer of Günter Zöchbauer
In short: It is not possible to access global variables from within the template files.
You have to at least create a getter that returns global variable.
In your typescript file create a getter called Math
get Math() {
return Math;
}
BUT I would either rename it to math or create a helper methods in my typescript file
How about augmenting the built-in Array object to use Math.max/Math.min instead:
Array.prototype.max = function() {
return Math.max.apply(null, this);
};
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
let p = [35,2,65,7,8,9,12,121,33,99];
console.log(`Max value is: ${p.max()}` +
`\nMin value is: ${p.min()}`);
Here is a JSFiddle.
Augmenting the built-ins can cause collisions with other libraries (some see), so you may be more comfortable with just apply'ing Math.xxx() to your array directly:
var min = Math.min.apply(null, arr),
max = Math.max.apply(null, arr);
Alternately, assuming your browser supports ECMAScript 6, you can use spread syntax which functions similarly to the apply method:
var min = Math.min( ...arr ),
max = Math.max( ...arr );
var max_of_array = Math.max.apply(Math, array);
For a full discussion see: http://aaroncrane.co.uk/2008/11/javascript_max_api/