-
카테고리
-
세부 분야
프로그래밍 언어
-
해결 여부
해결됨
enum과 객체 사용할 때의 차이점
22.09.08 13:29 작성 조회수 188
0
안녕하세요. 좋은 강좌 감사합니다.
const enum EDirection {
Up,
Down,
Left,
Right,
}
const ODirection = {
Up: 0,
Down: 1,
Left: 2,
Right: 3,
} as const;
EDirection.Up;
(enum member) EDirection.Up = 0
ODirection.Up;
(property) Up: 0
// Using the enum as a parameter
function walk(dir: EDirection) {}
// It requires an extra line to pull out the keys
type Direction = typeof ODirection[keyof typeof ODirection];
function run(dir: Direction) {}
walk(EDirection.Left);
run(ODirection.Right);
강의안에 있는 위의 예시를 실행해 보았는데요 마지막 줄에
walk(8); 을 실행하면 에러가 나지 않고
run(8); 은 에러가 나는 이유가 무엇일까요?
또 walk(Edirection.bottom); 이라고 실행하면 에러가 나던데 완전히
Edirection 안의 값과는 다른 8이라는 값을 넣었는데 에러가 안 나는 것이 이해가 안 됩니다.
답변을 작성해보세요.
답변 1