# ์—๋Ÿฌ ํ•ด๊ฒฐ๐Ÿ”‘ Catch clause variable type annotation must be 'any' or 'unknown' if specified.ts(1196)

์•„๋ž˜์™€ ๊ฐ™์ด ์—๋Ÿฌ ํ•ธ๋“ค๋ง์„ ํ•˜๋ ค๊ณ  ํ•  ๋•Œ์—๋Š” ํƒ€์ž…์„ ์–ด๋–ป๊ฒŒ ์ •์˜ํ•ด์ค„ ์ˆ˜ ์žˆ์„๊นŒ?

try {
  await auth().createUserWithEmailAndPassword(email, password);
} catch (error: unknown) {
  switch (
    error.code // Object is of type 'unknown'.ts(2571)
  ) {
    case 'auth/weak-password': {
      Alert.alert('Write a stronger password!');
    }
  }
}

error์— ๋Œ€ํ•œ ํƒ€์ž…์„ ์ •์˜ํ•ด์ฃผ๋ ค๊ณ  ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

interface AuthError {
  code: string;
  message: string;
}

try {
  await auth().createUserWithEmailAndPassword(email, password);
} catch (error) {
  // Catch clause variable type annotation must be 'any' or 'unknown' if specified.ts(1196)
  switch (error.code) {
    case 'auth/weak-password': {
      Alert.alert('Write a stronger password!');
    }
  }
}

try-catch ๊ตฌ๋ฌธ ์‚ฌ์šฉ ์‹œ, catch๋กœ ์žกํžˆ๋Š” error์— ๋”ฐ๋ผ ์—๋Ÿฌ ํ•ธ๋“ค๋ง์„ ํ•˜๋ ค๊ณ  ํ•  ๋•Œ. error์— ๋Œ€ํ•œ ํƒ€์ž… ์ •์˜๋ฅผ ํ•  ๊ฒฝ์šฐ์— ์ƒ๊ธฐ๋Š” ์—๋Ÿฌ๋‹ค. ํ•˜์ง€๋งŒ catch๋กœ ์žกํžˆ๋Š” ์—๋Ÿฌ์˜ ํƒ€์ž…์€ unexpectedํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜์˜ ์—๋Ÿฌ ํƒ€์ž…์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ํƒ€์ž…๊ฐ€๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

try {
  await auth().createUserWithEmailAndPassword(email, password);
} catch (error) {
  const isAuthError = (error: any): error is AuthError => {
    return typeof error.code === 'string';
  };

  if (isAuthError(error)) {
    switch (error.code) {
      case 'auth/weak-password': {
        Alert.alert('Write a stronger password!');
      }
    }
  } else {
    throw error;
  }
}
Last Updated: a year ago