inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

웹 게임을 만들며 배우는 React에 TypeScript 적용하기

useCallback과 keyof, typeof

undefined 처리

496

haruharu

작성한 질문수 43

0

강의에서 computerChoice의 리턴타입에 undefined가 추가되어있는 문제 해결하실때 if() throw new Error(); 사용해서 처리하거나 느낌표(!) 사용하신다했는데 if() throw new Error();로 어떻게 처리하신다는건지 알 수 있을까요? 어떻게 해봐도 해결이 안되네요. ㅠㅠ

 

const rspCoords = {
  바위: '0',
  가위: '-142px',
  보: '-284px',
} as const;

const scores = {
  가위: 1,
  바위: 0,
  보: -1,
} as const;

type imgCoords = typeof rspCoords[keyof typeof rspCoords];

const computerChoice = (imgCoords: imgCoords) => {
  return (Object.keys(rspCoords) as ['바위', '가위', '보']).find(function (v) {
    return rspCoords[v] === imgCoords;
  });
};

typescript react

답변 1

1

제로초(조현영)

const rspCoords = {
  바위: '0',
  가위: '-142px',
  보: '-284px',
} as const;

const scores = {
  가위: 1,
  바위: 0,
  보: -1,
} as const;

type imgCoords = typeof rspCoords[keyof typeof rspCoords];

const computerChoice = (imgCoords: imgCoords) => {
  const result = (Object.keys(rspCoords) as ['바위', '가위', '보']).find(function (v) {
    return rspCoords[v] === imgCoords;
  });
  if (!result) throw '말도 안돼';
  return result;
};

이렇게 가능합니다.

express response 타입

0

213

1

createRoot

0

288

1

babel-loader질문입니다! 왜쓰는지 궁금합니다!

0

687

1

compilerOptions lib 관련

0

330

1

event type 찾기

0

325

1

정확한 type vs 가독성

0

295

1

useRef에 대해 질문드립니다.

0

306

1

tsx파일에서 에러 표시

0

867

1

Props type 질문드립니다.

0

229

1

webpack.config.ts/ Could not find a declaration file for module '@pmmmwh/react-refresh-webpack-plugin'.

0

592

1

18버전에서의 ReactDOM.render

1

659

1

깃허브에 react-router@6 업데이트 해주신 코드 질문드립니다.

0

296

1

2강 끝말잇기 npx webpack 오류가 질의사항

0

411

1

Property 'render' does not exist on type 'IntrinsicAttributes & (PathRouteProps | LayoutRouteProps | IndexRouteProps)'.

0

717

1

webpack.config.ts import 할 때 에러좀 봐주세요!!

0

308

1

React.FC에 대해 궁금합니다.

0

702

1

가위바위보 interval 타입과 이벤트타입에 관한질문

0

345

1

npm run dev 실행은 성공적으로 되는데

0

680

2

redux 유료강좌를 듣고 와야 되는지 여쭈어보려고 합니다.

0

324

1

git 내용과 강의 내용이 달라서 여쭈어봅니다.

0

228

1

Ball.tsx 타이핑

0

329

2

react-router 버젼 업그레이드 변경사항 문의

0

290

2

리액트 타입스크립트 공식문서

0

341

1

button 클릭 할 떄 마다 더보기/닫기 에러

0

860

1