Answer the question
In order to leave comments, you need to log in
Why are angle brackets used in function names?
I think I understand why angle brackets are used in the function name, but I would like to make sure that I'm not mistaken. There is the following code:
function identity<T>(arg: T): T {
return arg;
}
let numberOutput = identity<Number>(1);
console.log(numberOutput); // выведет "1"
identity('qwerty')
function identity<T>(arg: T): T {
return arg;
}
let numberOutput = identity(1);
console.log(numberOutput);
Answer the question
In order to leave comments, you need to log in
The identity function takes a number and returns a number.
Thus, if the program has the following call to the identity('qwerty') function, then the programmer will notice the error at compile time, and not at runtime.
, the programmer would only notice the error at run time.
const x: number = identity('aaa')
//или
const x: string = identity(1)
// или любой другой вариант где типы не совпадут.
identity<number>(1)
identity(1)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question