타입 추론

Type inference

타입 검사기가 할당값과 사용 위치에서 타입을 계산합니다.

···
html
<div class="visual inference" data-step="0"><div class="visual-head"><strong>TYPE INFERENCE</strong><i></i></div><div class="visual-scene"><div class="infer-editor mono"><div>const n = <b>7</b></div><div class="infer-ghost">n: <strong>number</strong></div><div class="infer-bad">n.toUpperCase()</div></div><div class="infer-callout">type checker</div></div><div class="visual-caption"></div><div class="visual-dots"><b></b><b></b><b></b></div></div>
css
.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}
.inference .visual-scene{display:flex;align-items:center;justify-content:center;gap:8px}.infer-editor{width:min(72%,280px);border:1px solid var(--line);border-radius:7px;background:var(--bg);padding:8px 10px;display:flex;flex-direction:column;gap:6px;white-space:nowrap;font-size:12px}.infer-editor b{color:var(--accent)}.infer-ghost{opacity:.25;transition:opacity .3s}.infer-ghost strong{color:var(--accent)}.infer-bad{opacity:.3;transition:opacity .3s}.infer-callout{font-size:12px;color:var(--muted);max-width:25%}.inference[data-step="1"] .infer-ghost,.inference[data-step="2"] .infer-ghost,.inference[data-step="2"] .infer-bad{opacity:1}.inference[data-step="2"] .infer-bad{text-decoration:underline wavy var(--accent-3);text-underline-offset:3px}.inference[data-step="0"] .infer-editor div:first-child{background:color-mix(in srgb,var(--accent) 15%,var(--bg))}
js
const root=document.querySelector('.visual'),cap=root.querySelector('.visual-caption'),dots=[...root.querySelectorAll('.visual-dots b')],captions=["Read assigned value 7","Infer n as number","Reject a string method"];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가 변수로 전파되고 문자열 메서드 호출은 차단됩니다. 외부 API 경계나 공개 함수에서는 명시 타입이 의도를 더 잘 드러낼 수 있습니다.

언제 쓰나

지역 변수는 간결하게 두고, 외부 계약은 명시적으로 문서화할 때 활용합니다.

페이지로 열기 ↗