Array
const fruits: string[] = ['apple', 'banana'];
const scores: number[] = [1, 2, 3];
const scores2: Array<number> = [1, 3, 4];
const fruit: string[] = ['apple', 'banana'];
function printArray(fruit: readonly string[]) {
fruit.push('tomato'); *// fruit은 수정 할 수 없음 (Error)*
}
const fruit: string[] = ['apple', 'banana'];
function printArray(fruit: readonly Array<string>) { *// 이렇게는 아직 사용 불가*
fruit.push('tomato'); *// fruit은 수정 할 수 없음 (Error)*
}
Tuple → 튜플 대신 interface, type alias, class 를 이용하여 사용하는 걸 권장
*// 튜플 : 배열의 위치와 위치의 타입을 지정하는 배열*
let student: [string, number];
student = ['name', 20];
student[0];
student[1];
→ Tuple 사용을 권장하지 않음
→ 가독성이 떨어짐
let student: [string, number];
student = ['name', 20];
const [name, age] = student;
*// const [a] -> const a = student[0];
// const [b, c] -> const b = student[0]; / const c = student[1];*
→ 그래도 사용 시 방법(가독성 약간 상승)
→ 이름으로 타입을 추측할 수 있어서 그나마 가독성 향상
→ 그 외 방법 : React SimpleHabit API안에 있는 useState() 함수 이용