Array<Type> VS Type[] in Typescript 1

Array VS Type[] in Typescript

function loggingIdentity(arg: T[]): T[] {
    console.log(arg.length);
    return arg;
}
// or 
function loggingIdentity(arg: Array): Array {
    console.log(arg.length);
    return arg;
}

Here is what the above code is Doing:
1. We’ve added a type variable T to the first parameter arg.
2. We’ve added T again as the type of the parameter arg.
3. We’ve added T again as the return type of the function.

This is a bit repetitive. We can actually omit the type on the parameters, and the compiler will just infer them from the type of the arguments passed in.

Similar Posts