Among Us - Crewmates
 

[TypeScript] 16. number 인덱슀 μ‹œκ·Έλ‹ˆμ²˜λ³΄λ‹€λŠ” Array, νŠœν”Œ, ArrayLike μ‚¬μš©ν•˜κΈ°

728x90

πŸ“š Effective TypeScript 책을 읽고 κ³΅λΆ€ν•œ λ‚΄μš©μ„ μ •λ¦¬ν•œ κΈ€μž…λ‹ˆλ‹€.

number 인덱슀 μ‹œκ·Έλ‹ˆμ²˜λ³΄λ‹€λŠ” Array, νŠœν”Œ, ArrayLike μ‚¬μš©ν•˜κΈ°

μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ κ°μ²΄λž€?

  • ν‚€/κ°’ 쌍의 λͺ¨μŒ
    • ν‚€λŠ” 보톡 λ¬Έμžμ—΄
    • 값은 μ–΄λ–€ 무엇이든 될 수 μžˆλ‹€

  • 파이썬, μžλ°”μ—μ„œ λ³Ό 수 μžˆλŠ” β€˜ν•΄μ‹œ κ°€λŠ₯’ 객체 ν‘œν˜„μ΄ μžλ°”μŠ€ν¬λ¦½νŠΈμ—λŠ” μ—†λ‹€.
    • toString λ©”μ„œλ“œκ°€ ν˜ΈμΆœλ˜μ–΄ 객체가 λ¬Έμžμ—΄λ‘œ λ³€ν™˜λ˜μ–΄ 버린닀.
> x={}
{}
> x[[1,2,3]] = 2
2
> x
{ '1,2,3': 1 }

  • μˆ«μžλŠ” ν‚€λ‘œ μ‚¬μš©ν•  수 μ—†λ‹€.
    • μžλ°”μŠ€ν¬λ¦½νŠΈ λŸ°νƒ€μž„μ΄ λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•΄ 버린닀.
> { 1: 2, 3: 4 }
{ '1': 2, '3': 4 }

  • 배열은 λΆ„λͺ…νžˆ 객체이닀
    • λ•Œλ¬Έμ— 숫자 인덱슀λ₯Ό μ‚¬μš©ν•˜λŠ” 것이 λ‹Ήμ—°ν•˜λ‹€.
> typeof []
'object'

> x = [1, 2, 3]
[1, 2, 3]
> x[0]
1

  • μ•žμ˜ μΈλ±μŠ€λ“€μ€ λ¬Έμžμ—΄λ‘œ λ³€ν™˜λ˜μ–΄ μ‚¬μš©λœλ‹€.
    • λ¬Έμžμ—΄ ν‚€λ₯Ό μ‚¬μš©ν•΄λ„ μ—­μ‹œ λ°°μ—΄μ˜ μš”μ†Œμ— μ ‘κ·Όν•  수 μžˆλ‹€.
> x['1']
2

  • Object.keys λ₯Ό μ΄μš©ν•΄ λ°°μ—΄μ˜ ν‚€λ₯Ό λ‚˜μ—΄ν•˜λ©΄, ν‚€κ°€ λ¬Έμžμ—΄λ‘œ 좜λ ₯λœλ‹€.
> Object.keys(x)
[ '0', '1', '2' ]

λ”°λΌμ„œ, μ΄λŸ¬ν•œ ν˜Όλž€μ„ λ°”λ‘œμž‘κΈ° μœ„ν•΄ νƒ€μž…μŠ€ν¬λ¦½νŠΈλŠ” 숫자 ν‚€λ₯Ό ν—ˆμš©ν•˜κ³ , λ¬Έμžμ—΄ 킀와 λ‹€λ₯Έ κ²ƒμœΌλ‘œ μΈμ‹ν•œλ‹€.
interface Array<T> {
	// ...
	[n: number]: T;
}

  • λ¬Έμžμ—΄ ν‚€λ‘œ μΈμ‹ν•˜λ―€λ‘œ, νƒ€μž… 체크 μ‹œμ μ— 였λ₯˜λ₯Ό μž‘μ„ 수 μžˆλ‹€.
    • μ‹€μ œλ‘œ λ™μž‘ν•˜μ§€ μ•ŠλŠ” μ½”λ“œμ΄λ‹€.
    • νƒ€μž… μ‹œμŠ€ν…œμ˜ λ‹€λ₯Έ 것듀과 λ§ˆμ°¬κ°€μ§€λ‘œ, νƒ€μž… μ •λ³΄λŠ” λŸ°νƒ€μž„μ— μ œκ±°λœλ‹€.
const xs = [1, 2, 3];
const x0 = xs[0];  // OK
const x1 = xs['1'];
           // ~~~ Element implicitly has an 'any' type
           //      because index expression is not of type 'number'

function get<T>(array: T[], k: string): T {
  return array[k];
            // ~ Element implicitly has an 'any' type
            //   because index expression is not of type 'number'
}

  • Object.keys 같은 ꡬ문은 μ—¬μ „νžˆ λ¬Έμžμ—΄λ‘œ λ°˜ν™˜λœλ‹€.
    • string이 number에 할당될 수 μ—†κΈ° λ•Œλ¬Έμ—, λ§ˆμ§€λ§‰ 쀄이 μ΄μƒν•˜κ²Œ 보일 수 μžˆλ‹€.

      β‡’ 배열을 μˆœνšŒν•˜λŠ” μ½”λ“œ μŠ€νƒ€μΌμ— λŒ€ν•œ μ‹€μš©μ μΈ ν—ˆμš©μ΄λΌ μƒκ°ν•˜λ©΄ μ’‹λ‹€ !

      β‡’ μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œλŠ” ν”ν•œ μΌμ΄μ§€λ§Œ, 배열을 μˆœνšŒν•˜κΈ° 쒋은 방법은 μ•„λ‹ˆλ‹€.

const xs = [1, 2, 3];
const keys = Object.keys(xs);  // Type is string[]
for (const key in xs) {
  key;  // Type is string
  const x = xs[key];  // Type is number
}

  • μΈλ±μŠ€μ— μ‹ κ²½ 쓰지 μ•ŠλŠ”λ‹€λ©΄, for-of λ₯Ό μ‚¬μš©ν•˜λŠ” 것이 μ’‹λ‹€.
const xs = [1, 2, 3];
for (const x of xs) {
  x;  // Type is number
}
  • 인덱슀의 νƒ€μž…μ΄ μ€‘μš”ν•˜λ©΄, number νƒ€μž…μ„ μ œκ³΅ν•΄μ€„ forEachλ₯Ό μ‚¬μš©ν•˜λ©΄ λœλ‹€.
const xs = [1, 2, 3];
xs.forEach((x, i) => {
  i;  // Type is number
  x;  // Type is number
});
  • 루프 쀑간에 λ©ˆμΆ°μ•Ό ν•œλ‹€λ©΄, C μŠ€νƒ€μΌμ˜ for(;;) 루프가 μ’‹λ‹€.
const xs = [1, 2, 3];
for (let i = 0; i < xs.length; i++) {
  const x = xs[i];
  if (x < 0) break;
}

πŸ’‘
νƒ€μž…μ΄ λΆˆν™•μ‹€ν•˜λ‹€λ©΄, λŒ€λΆ€λΆ„μ˜ λΈŒλΌμš°μ €μ™€ μžλ°”μŠ€ν¬λ¦½νŠΈ μ—”μ§„μ—μ„œ for-in λ£¨ν”„λŠ” for-of λ˜λŠ” C μŠ€νƒ€μΌμ˜ for(;;) 루프에 λΉ„ν•΄ λͺ‡ λ°°λ‚˜ λŠλ¦¬λ‹€.

β‡’ μ„±λŠ₯ 체크 κ°€λŠ₯ν•œμ§€ 확인

인덱슀 μ‹œκ·Έλ‹ˆμ²˜κ°€ number둜 ν‘œν˜„λ˜μ–΄ μžˆλ‹€λ©΄

  • μž…λ ₯ν•œ 값이 number μ—¬μ•Ό ν•œλ‹€λŠ” 것을 μ˜λ―Έν•˜μ§€λ§Œ (for-in 루프 μ œμ™Έ)
  • μ‹€μ œ λŸ°νƒ€μž„μ— μ‚¬μš©λ˜λŠ” ν‚€λŠ” string νƒ€μž…

일반적으둜 string λŒ€μ‹  numberλ₯Ό νƒ€μž…μ˜ 인덱슀 μ‹œκ·Έλ‹ˆμ²˜λ‘œ μ‚¬μš©ν•˜λŠ” κ²½μš°λŠ” λ§Žμ§€ μ•Šλ‹€.

ν•˜μ§€λ§Œ λ§Œμ•½ 숫자λ₯Ό μ‚¬μš©ν•΄μ„œ 인덱슀 ν•­λͺ©μ„ μ§€μ •ν•œλ‹€λ©΄

  • Array λ˜λŠ” νŠœν”Œ νƒ€μž…μ„ λŒ€μ‹  μ‚¬μš©ν•˜κ²Œ 될 것이닀.

μ–΄λ– ν•œ 길이λ₯Ό κ°€μ§€λŠ” λ°°μ—΄κ³Ό λΉ„μŠ·ν•œ ν˜•νƒœμ˜ νŠœν”Œμ„ μ‚¬μš©ν•˜κ³  μ‹Άλ‹€λ©΄

β‡’ νƒ€μž…μŠ€ν¬λ¦½νŠΈμ— μžˆλŠ” ArrayLike νƒ€μž…μ„ μ‚¬μš©ν•œλ‹€.

const xs = [1, 2, 3];
function checkedAccess<T>(xs: ArrayLike<T>, i: number): T {
  if (i < xs.length) {
    return xs[i];
  }
  throw new Error(`Attempt to access ${i} which is past end of array.`)
}

ArrayLike λ₯Ό μ‚¬μš©ν•˜λ”λΌλ„ ν‚€λŠ” μ—¬μ „νžˆ λ¬Έμžμ—΄

const xs = [1, 2, 3];
const tupleLike: ArrayLike<string> = {
  '0': 'A',
  '1': 'B',
  length: 2,
};  // OK

Uploaded by N2T

728x90
λ°˜μ‘ν˜•