Issue
Going through the TS handbook. https://www.typescriptlang.org/docs/handbook/2/functions.html
In TypeScript, generics are used when we want to describe a correspondence between two values. We do this by declaring a type parameter in the function signature:
function firstElement<Type>(arr: Type[]): Type | undefined {
return arr[0];
}
By adding a type parameter Type to this function and using it in two places, we’ve created a link between the input of the function (the array) and the output (the return value). Now when we call it, a more specific type comes out:
In this case is Type
a keyword? When I see just T
is that the same thing?
Am I understanding this section correctly? By adding we are telling Typescript to assign whatever type the input is to the output?
Solution
In this case is
Type
a keyword?
In your example, Type
is a generic type parameter (type variable).
When I see just
T
is that the same thing?
Assuming the token is in the same position, yes.
By adding we are telling Typescript to assign whatever type the input is to the output?
It's helpful for me to think about them analogous to parameters in functions, but for types. You're effectively telling typescript this:
"I'm creating a type parameter (variable) called Type
. I'm going to accept an argument in this function which will be an array, and you should use the type of each element in the array as the real type in place of Type
. I'm going to return one of the array elements, so the return type of this function should be the same type as one of the array elements (or undefined
if I use an index at which there's no value)."
Now, there's one more potentially confusing part to this, based on the example that you provided, which is "Why doesn't TypeScript account for the possibility of undefined when indexing an array element using square brackets?", and that's covered in this answer.
Here's a concrete example:
function firstElement<Type>(arr: Type[]): Type | undefined {
return arr[0];
}
// This type is: number[]
const arr1 = [1, 2, 3];
// When giving `arr1` as the argument to `firstElement`,
// `Type` will become `number` because each element in `arr1`
// is `number`. So the result will be: number | undefined
const result1 = firstElement(arr1);
// This type is: string[]
const arr2 = ['hello', 'world'];
// When giving `arr2` as the argument to `firstElement`,
// `Type` will become `string` because each element in `arr2`
// is `string`. So the result will be: string | undefined
const result2 = firstElement(arr2);
Here's another example using T
instead of Type
for the generic type parameter:
function elementAtIndex<T>(arr: T[], index: number): T | undefined {
return arr[index];
}
const arr = ['zero', 'one', 'two'];
const zero = elementAtIndex(arr, 0); // string | undefined
console.log(zero); // "zero"
const three = elementAtIndex(arr, 3); // string | undefined
console.log(three) // undefined
FWIW,
elementAtIndex
in the example above is just a functional version ofArray.prototype.at()
, which should be in an upcoming release of TypeScript (probablyv4.6
).
Answered By - jsejcksn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.