유니언 타입은 number | string처럼 가능한 값의 집합을 합칩니다. 값을 받는 곳은 어느 쪽이 와도 안전한 연산만 바로 쓸 수 있습니다.
데모는 값이 숫자와 문자열로 바뀌어도 공통 연산만 열려 있다가 검사 후 전용 연산이 나타나는 흐름입니다. 분기 없이 한 타입의 메서드만 호출하면 안전하지 않습니다.
언제 쓰나
API 응답이나 입력값의 가능한 상태를 하나의 타입으로 명시할 때 사용합니다.
값이 여러 타입 중 하나일 수 있음을 선언합니다.
<div class="visual union" data-step="0"><div class="visual-head"><strong>UNION TYPE</strong><i></i></div><div class="visual-scene"><div class="set-circle set-a">number</div><div class="set-circle set-b">string</div><div class="set-value mono">value: 7</div></div><div class="visual-caption"></div><div class="visual-dots"><b></b><b></b><b></b><b></b></div></div>.visual{width:min(95vw,760px);height:min(91vh,330px);padding:clamp(9px,2.5vmin,18px);display:flex;flex-direction:column;gap:clamp(6px,1.7vmin,11px);border:1px solid var(--line);border-radius:13px;background:var(--surface);font:500 clamp(12px,3.4vmin,16px)/1.25 var(--font-sans,-apple-system),sans-serif}
.visual-head{display:flex;align-items:center;justify-content:space-between;gap:8px;min-height:19px;color:var(--accent)}.visual-head strong{font-size:1em}.visual-head i{display:block;width:8px;height:8px;flex:none;border-radius:50%;background:var(--accent);animation:visual-pulse 1.3s infinite}@keyframes visual-pulse{50%{opacity:.25}}
.visual-scene{position:relative;flex:1;min-height:0;overflow:hidden}.visual-caption{min-height:2.4em;display:grid;place-items:center;text-align:center}.visual-dots{display:flex;justify-content:center;gap:4px}.visual-dots b{display:block;width:15px;height:3px;border-radius:3px;background:var(--line)}.visual-dots b.on{background:var(--accent)}
.visual-scene *{box-sizing:border-box}.visual-scene .mono{font-family:ui-monospace,monospace}.visual-scene .muted{color:var(--muted)}.visual-scene .lit{border-color:var(--accent)!important;background:color-mix(in srgb,var(--accent) 14%,var(--surface))!important;color:var(--fg)!important}
.union .visual-scene{display:flex;align-items:center;justify-content:center;padding-bottom:22px}.set-circle{width:75px;height:75px;border:2px solid var(--accent-2);border-radius:50%;display:grid;place-items:center;position:relative;font-size:12px;background:color-mix(in srgb,var(--accent-2) 9%,transparent);transition:background .3s,transform .3s}.set-b{margin-left:-20px;border-color:var(--accent-3);background:color-mix(in srgb,var(--accent-3) 9%,transparent)}.set-value{position:absolute;bottom:0;left:0;right:0;text-align:center;font-size:12px}.union[data-step="0"] .set-a,.union[data-step="2"] .set-a{background:color-mix(in srgb,var(--accent-2) 27%,var(--surface));transform:translateX(-4px)}.union[data-step="1"] .set-b,.union[data-step="3"] .set-b{background:color-mix(in srgb,var(--accent-3) 27%,var(--surface));transform:translateX(4px)}@media(min-width:600px){.set-circle{width:120px;height:120px}.set-b{margin-left:-34px}}const root=document.querySelector('.visual'),cap=root.querySelector('.visual-caption'),dots=[...root.querySelectorAll('.visual-dots b')],captions=["Number is one possibility","String is another","Check before number methods","Check before string methods"];let step=0;function advance(){root.dataset.step=String(step);cap.textContent=captions[step];dots.forEach((d,i)=>d.classList.toggle('on',i===step));if(root.classList.contains('generic')){root.querySelector('.generic-input').textContent=step===3?'"hi"':'7';root.querySelector('.generic-output').textContent=step===3?'"hi" : string':'7 : number'}if(root.classList.contains('union'))root.querySelector('.set-value').textContent=step%2?'value: "seven"':'value: 7';step=(step+1)%captions.length}advance();let timer=setInterval(advance,1100);root.addEventListener('pointerdown',()=>{clearInterval(timer);advance()})유니언 타입은 number | string처럼 가능한 값의 집합을 합칩니다. 값을 받는 곳은 어느 쪽이 와도 안전한 연산만 바로 쓸 수 있습니다.
데모는 값이 숫자와 문자열로 바뀌어도 공통 연산만 열려 있다가 검사 후 전용 연산이 나타나는 흐름입니다. 분기 없이 한 타입의 메서드만 호출하면 안전하지 않습니다.
API 응답이나 입력값의 가능한 상태를 하나의 타입으로 명시할 때 사용합니다.