Type inference

타입 추론

A type checker derives types from assignments and usage context.

···
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()})

Type inference derives a type from values and context without requiring every declaration to spell it out. The inferred type then constrains later operations.

The demo propagates number from a value to a variable and rejects a string method call. Explicit types can still clarify public functions and API boundaries.

When to use

Use it to keep local code concise while documenting external contracts explicitly.

Open as page ↗