Typescript

kiko·2021년 7월 12일

Typesciprt

  • Application이 커질수록 undefined나 TypeError 로 디버깅에 많은 시간을 할애 할 수 있다. 왜냐하면 Javascript는 Exception을 던지지 않고 결과를 만들어 낸다.

  • interface 또는 genric으로 개발자들에게 문서화를 제공하고, 안전한 프로그램을 구현.

  • javascript 프로젝트로 드문드문 타입을 정의 했음. (예를 들어 object로 타입을 선언 했지만 어떤 fields를 가지고 있는 묘사가 부족할 수 있음.)

Component의 props, state 타입 정의

generaic 또는 interface을 이용해 타이핑 해줄 수 있다.

interface IButtonProps {
  title: string;
  onClick: () => void;
}

type ButtonState = {
  disabled: boolean;
};

class Button extends React.Component<IButtonProps, ButtonState> {
  // ...
}

API response에 타입 정의

export interface ILive {
  access_key: string;
  author_id: number;
  cast_id: number;
  categories: string[];
  close_status: number;
  closed: string;
  created: string;
  engine: string;
}

상태관리에 타입 정의

상태관리는 state, action에 타입 정의가 필요하다.

export type ToastState = {
  isVisible: boolean;
};

const initialToastState: ToastState = {
  isVisible: false
};
profile
무를 심자.

0개의 댓글