-
Notifications
You must be signed in to change notification settings - Fork 0
Udemy/Ts/section2/20 #24
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 작성한 커밋입니다.
Github에서 작성한 커밋입니다.
Github에서 수정한 커밋입니다.
Github에서 수정한 커밋입니다.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
✍Udemy/Ts/section2/20
🔗Reference
🔥KeyWord
📝Description
eunm
enum
을 사용해서 간단하고 쉽게 원하는 값을 할당하고 객체의 형태로 관리 할수 있다. 단. 열겨형과 객체의 차이를 잘 살펴 보아야한다.error TS2367
이 문제가 발생한 코드는
person.role = Role.READ_ONLY;
이 부분인데person
이라는 객체에서role
속성에 값을 할당 해서 발생한 상황이었다.enum overlap
person.role
이Role.READ_ONLY
로 설정된 후 TypeScript는person.role
이 더 이상Role.ADMIN
과 같은 값이 될 수 없다고 판단하는 것이다. 즉,person.role = Role.READ_ONLY;
로 재할당을 했음에도 TypeScirpt에서는 이를 받아 들이지 않는 것이다. 그래서 TypeScript는 혼란이 오게 되는 것이고 초기 설정값을 따르려고 하기 때문에 발생한 오류인것이다. 따라role: Role.ADMIN
과 같이 초기 값을 부여하게 되면as const
로 값이 변하지 않게 명시를 하는 것을 권장한다.역방향 매핑
이 있다. TypeScript의 enum은 숫자형 값일 경우 역방향 매핑을 지원한다. 즉, Role.ADMIN으로 0을 얻을 수 있으며 바대로 0으로도 Role.ADMIN을 얻을 수 있다. 이는 일반 객체로는 구현하기 어렵다.===
연산으로 비교 하면 아무런 의미가 없다고 판단을 한다 왜냐하면 이미 서로 다른 enum임을 인지하고 논리적으로 명확하기 때문에 overlap이 발생하는 것이다.person.role = Role.READ_ONLY;
는?논리 구조상 초기 할당 값은취소선은 내가 TypeScript의 입자에서 생각해본 문장이다. 크게 의미가 없으며 결론적으로 다음과 같은 원일 가능성이 있다.enum Role
에서 부터의 ADMIN이였다. 이후 READ_ONLY로 재할당이 되었다. 그렇다면 이 둘은 같은 타입, 값일 것이라는 논리가 애초에 성립이 되지 않는다는 것이다. 즉, 이미 같은 타입, 중복되는 타입, 값이라는 것을 인지하고 있기 때문인 것이다. 따라서 TypeScript에서 타입 추론으로 인해서 불필요한 메모리 낭비를 하지 않기 위해 비교 연산을 최소화 하는 것이라고 판단 할 수 있다.📌Summary
error TS2367
가 발생한 원인을 명확하게 이해할 수 없어서 많이 답답했다. 참고한 자료를 살펴보면 TypeScript는 person.role 변수가 Role.ADMIN으로 초기화된 후 다른 Role 값으로 변경될 수 있음에도 이 변경 가능성을 제대로 추론하지 못해 생겨난 오류로 판단 되었다. 따라서 TypeScript가 person에 정의한 값이 충분히 동적으로 타입 추론할 수 있게 하거나const person
에서 선언한 자료형const
에서 벗어지 못하게as const
로 명시하는 것이 최선이라는 것이 나의 결론이다. 이는 동적인 js에 익숙한 사용자들이 쉽게 겪을 수 있는 오류와 논리 문제로 판단이 된다. 아직 많이 미흡하고 명확한 해답을 찾지 못해서 아쉽다.