📝 Edit page
➕ Add page
Generics
Resources
- Generics in the handbook.
As in C# and Java, you can create a component that can work over a variety of types, which makes them resuable.
Function
From the handbook.
Instead of setting the type as say number
or any
.
function identity(arg: number): number {
return arg;
}
You can let it be dynamic.
function identity<Type>(arg: Type): Type {
return arg;
}
You can use like this:
let foo = identity("bar");
Which implies passing the type string
as the value of T
.
let foo: string = identity<string>("bar")
Type alias
You can create a generic type.
Here is an array which can have any type of item inside it.
type MyTypeAlias<T> = Array<T>
Or a hash.
type Container<T> = { myKey: T };
Examples
Equals
Check if all items in an array are equal.
function equal<Type>(items: Array<Type>) { // OR Type[]
return items.every(i => i === items[0])
}
Pass an array of numbers.
const x = [1, 1, 1]
equal(x)
// true
const y = [1, 2, 3]
equal(x)
// false
Pass an array of strings or of mixed types.
equal(["1", "1", "1"])
// true
equal(["1", "1", 1])
// true
If you want to enforce all the types be equal, then pass the type explicitly.
equal<string>(["1", "1", 1])
// Argument of type '(string | number)[]' is not assignable to parameter of type 'string[]'.
// Type 'string | number' is not assignable to type 'string'.
// Type 'number' is not assignable to type 'string'.