xxxxxxxxxx
function identity<T>(arg: T): T {
return arg;
}
let fun = identity<string>("hello");
console.log(fun);
/*T is shorthand for Type, meaning "you can specify the data type later"*/
xxxxxxxxxx
/*
What are Generics?
Generics have been a major feature of strongly typed languages
like Java and C#. In TypeScript, they allow the types of components
and functions to be "SPECIFIED LATER" which allows them to be used
in creating reusable components that can apply to different use cases,
for example:
*/
function returnInput <Type>(arg: Type): Type {
return arg;
};
const returnInputStr = returnInput<string>('Foo Bar');
const returnInputNum = returnInput<number>(5);
console.log(returnInputStr); // Foo Bar
console.log(returnInputNum); // 5
xxxxxxxxxx
function identity<T>(arg: T): T {
return arg;
}
let output1 = identity<string>("myString");
let output2 = identity<number>( 100 );
console.log(output1);
console.log(output2);
xxxxxxxxxx
function firstElement<Type>(arr: Type[]): Type {
return arr[0];
}
// s is of type 'string'
const s = firstElement(["a", "b", "c"]);
// n is of type 'number'
const n = firstElement([1, 2, 3]);
xxxxxxxxxx
function identity<Type>(arg: Type): Type {
return arg;
}
let myIdentity: <Input>(arg: Input) => Input = identity;
xxxxxxxxxx
type staffsKeys = {
name: string,
salary: number
}
function getProperty<T extends staffsKeys, K extends keyof staffsKeys>(obj: T, key: K): T[K] {
return obj[key];
}
xxxxxxxxxx
class Greeter<T> {
greeting: T
constructor(message: T) {
this.greeting = message
}
}
let greeter = new Greeter<string>('Hello, world')
xxxxxxxxxx
const clone = <T>(object: T) => {
const clonedObject: T = JSON.parse(JSON.stringify(object));
return clonedObject;
};
const obj = {
a: 1,
b: {
c: 3,
},
};
const obj2 = clone(obj);
xxxxxxxxxx
function firstElement<Type>(arr: Type[]): Type | undefined {
return arr[0];
}