반응형
문제 1
/*
1. myDiverseArray와 anotherDiverseArray에 적용할 수 있는 제네릭 타입 MyDiverseArray를 만드세요.
힌트: 타입도 변수처럼 type alias로 선언 가능.
*/
type MyDiverseArray = {};
const myDiverseArray = [true, 100, 101, 102, false];
const anotherDiverseArray = [
myDiverseArray,
"backend developer",
{ name: "hermione" },
];
나의 풀이
type MyDiverseArray<T> = T[];
type Name = {
name : string
}
const myDiverseArray : MyDiverseArray<boolean | number> = [true, 100, 101, 102, false];
const anotherDiverseArray : MyDiverseArray<MyDiverseArray<boolean | number> | string | Name> = [
myDiverseArray,
"backend developer",
{ name: "hermione" },
];
문제 2
/*
2. stringOnlyArray와 mixedArray에 적용할 수 있는 제네릭 타입 WithDefaultType을 만들되
stringOnlyArray에는 타입 인자를 전달하지 않아도 컴파일이 되도록 하세요.
*/
type WithDefaultType = {};
const stringOnlyArray = ["we're", "all", "gonna", "make", "it"];
const mixedArray = [2023, "year of the rabbit"];
나의 풀이
type WithDefaultType<T> = T[];
const stringOnlyArray : WithDefaultType<string> = ["we're", "all", "gonna", "make", "it"];
const mixedArray : WithDefaultType<number | string> = [2023, "year of the rabbit"];
문제 3
/*
3. createTupleTriplet이라는 함수가 있습니다.
이 함수는 세개의 인자를 전달받을 수 있고, 전달받은 인자를 순서대로 배열/튜플에 담아 반환을 합니다.
제네릭을 써서 함수를 완성하세요. (타입 변수, 인자 타입, 반환 타입)
*/
export function createTupleTriplet(firstValue, secondValue, thirdValue) {}
나의 풀이
export function createTupleTriplet<T,V,A>(
firstValue : T,
secondValue : V,
thirdValue : A
) : [T, V, A] {
return [firstValue, secondValue, thirdValue]
}
반응형
'JavaScript&TypeScript' 카테고리의 다른 글
[JS] Promise (0) | 2023.02.12 |
---|---|
[JS] var, let, const (0) | 2023.02.07 |
TypeScript Generic 사용해보기 (0) | 2023.01.27 |
TypeScript 미리 써보는 공식사이트 (0) | 2023.01.27 |
TypeScript enum 너란 존재 . (0) | 2023.01.20 |
댓글