TS Generic 연습
import { useState } from "react";
const getString = (args: string): string => {
return args;
};
const result1 = getString("철수");
const getNumber = (args: number): number => {
return args;
};
const result2 = getString("철수");
const getAny2 = (args: any) => {
return args;
};
const result3_1 = getAny2("철수");
const result3_2 = getAny2(8);
const result3_3 = getAny2(true);
const getAnys = (arg1: any, arg2: any, arg3: any): [any, any, any] => {
return [arg3, arg2, arg1];
};
const result4 = getAnys("철수,true, 8");
function getGeneric<MyType>(args: MyType): MyType {
return args;
}
const aaa: string = "철수";
const bbb: number = 8;
const ccc: boolean = true;
const result5_1 = getGeneric(aaa);
const result5_2 = getGeneric(bbb);
const result5_3 = getGeneric(ccc);
function getGenerics<MyType1, MyType2, MyType3>(arg1: MyType1,arg2: MyType2,arg3: MyType3): [MyType3, MyType2, MyType1] {
return [arg3, arg2, arg1];
}
const result6 = getGenerics("철수", true, 8);
function getGenericsT<T1, T2, T3>(arg1: T1, arg2: T2, arg3: T3): [T3, T2, T1] {
return [arg3, arg2, arg1];
}
const result7 = getGenericsT("철수", true, 8);
function getGenericsTUV<T, U, V>(arg1: T, arg2: U, arg3: V): [V, U, T] {
return [arg3, arg2, arg1];
}
const result8 = getGenericsTUV("철수", true, 8);
const [aaa, setAAA] = useState<number>(111);
const getGenericsArrow = <T, U, V>(arg1: T, arg2: U, arg3: V): [V, U, T] => {
return [arg3, arg2, arg1];
};
const result10 = getGenericsArrow("철수", true, 8);