Code Logo

Array Generic Annotations

Published at24 Jul 2026
TypeScript Functions Easy 0 views
Like0

Write a TypeScript function that uses the generic Array<string> syntax. Create a function that joins an array of strings with a separator. The parameter must be typed as Array<string>.

TypeScript supports two syntaxes for typed arrays: string[] and Array<string>. The generic Array<T> syntax is consistent with other generic types like Promise<T> and Map<K, V>.

Using the generic Array syntax makes the type parameter explicit and is preferred in some TypeScript style guides for consistency with other generic types.

The generic Array syntax works seamlessly with other generic patterns. For example, Promise> clearly represents a promise that resolves to a string array. The generic syntax is essential when working with complex types like Observable> or Map>.

The generic Array syntax works seamlessly with other generic patterns. For example, Promise<Array<string>> clearly represents a promise that resolves to a string array. The generic syntax is essential when working with complex types like Observable<Array<User>> or Map<string, Array<number>>.

TypeScript's array types support all standard JavaScript array methods with proper type inference. Methods like push(), pop(), map(), and filter() are fully typed, returning the correct element type.

The Array<T> syntax is particularly useful when T is a complex type, making the code more readable than T[] in some contexts. TypeScript style guides often prefer the generic syntax for consistency.

Example Input & Output

Example 1
Input
["x","y"], " "
Output
"x y"
Example 2
Input
["a","b","c"], ","
Output
"a,b,c"

Algorithm Flow

Recommendation Algorithm Flow for Array Generic Annotations
Recommendation Algorithm Flow for Array Generic Annotations

Solution Approach

Write a TypeScript function that uses the generic Array<string> syntax. Create a function that joins an array of strings with a separator. The parameter must be typed as Array<string>.

TypeScript supports two syntaxes for typed arrays: string[] and Array<string>. The generic Array<T> syntax is consistent with other generic types like Promise<T> and Map<K, V>.

Using the generic Array syntax makes the type parameter explicit and is preferred in some TypeScript style guides for consistency with other generic types.

Best Answers

typescript - Approach 1
function joinStrings(items: Array<string>, separator: string): string {
    if (items.length === 0) return '';
    var result = items[0];
    for (var i = 1; i < items.length; i++) {
        result += separator + items[i];
    }
    return result;
}