Skip to content

Udemy/Ts/section2/29 #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Sep 2, 2024
Merged

Udemy/Ts/section2/29 #33

merged 33 commits into from
Sep 2, 2024

Conversation

4BFC
Copy link
Member

@4BFC 4BFC commented Sep 2, 2024

✍Udemy/Ts/section2/29

본 PR은 청강 1회독을 마친 후 N회독으로 작성된 PR입니다.

🔥KeyWord

  • Unknown Type
  • Any Type

📝Description

  • Unknown Type
  • Any Type과 Unknown Type의 차이
  • 🚩: unknown Type으로 할당된 값을 if문을 통해 안정적으로 사용할 수 있는 예시
  • 위 코드를 참고하면서 설명하면 userInputunknown 타입으로 할당되어 있는 상황에서 string 타입이 할당된 userNameuserName = userInput;으로 재할당하게 되면 error ts(2322)를 반환한다. 이유는 다음과 같다.
    let userName: string;
    userInput = 5;
    userInput = 'Max';
    
    앞서 userInput의 마지막 할당 타입은 string이다. 같은 타입을 가진 두 변수가 왜 할당할 수 없을까?
    • unknown Type은 unknown이다.
    • literal Type을 확인해보면 userInputunknown으로 타입이 할당되어 있는 것을 알 수있다. 이는 any와 달리 타입의 안정성을 확보하기 위함이다. 따라 타입을 좁혀나가는 방식이 무조건 적으로 필요하다는 것이다. 따라 if문과 같이 조건문에 typeof를 사용해서 타입을 좁혀나가야unknown타입을 명확히 사용할 수 있다. 결론적으로 unknownany와 다르게 unknown의 타입을 강건하게 고수는 타입이라는 것을 알 수 있다.

📌Summary

  • Ts 개발자들 중 any를 난무하는 개발자들이 많다는 글을 읽어본적 있다. 명확히 타입의 값이 정해지지 않았다면 any가 아닌 unknow과 같이 타입의 안정성을 보장해주는 방식을 사용하는 것이 좋다는 것을 알았다.

4BFC and others added 30 commits August 27, 2024 01:50
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 작성한 커밋입니다.
Github에서 작성한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
@4BFC 4BFC added the 2회독 회독 label Sep 2, 2024
@4BFC 4BFC added this to the Udemy:TypeScript milestone Sep 2, 2024
@4BFC 4BFC self-assigned this Sep 2, 2024
@4BFC 4BFC merged commit e790cab into UdemyTs Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2회독 회독
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant