널 가능성

Nullability

값이 없을 수 있음을 타입에 담아 사용 전에 검사합니다.

···
html
<div class="visual nullable" data-step="0"><div class="visual-head"><strong>NULL CHECK</strong><i></i></div><div class="visual-scene"><div class="null-source mono">User | null</div><div class="null-fork"><div class="null-value">User<br><strong>use name</strong></div><div class="null-empty">null<br><strong>stop</strong></div></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}
.nullable .visual-scene{display:flex;flex-direction:column;align-items:center;justify-content:center;gap:9px}.null-source{border:1px solid var(--line);border-radius:7px;background:var(--bg);padding:6px 10px}.null-fork{width:100%;display:flex;justify-content:center;gap:20px}.null-fork div{width:37%;border:1px solid var(--line);border-radius:7px;text-align:center;padding:7px 4px;background:var(--bg);transition:opacity .3s,transform .3s}.null-fork strong{font-size:12px}.nullable[data-step="0"] .null-source,.nullable[data-step="1"] .null-empty,.nullable[data-step="2"] .null-value{border-color:var(--accent);background:color-mix(in srgb,var(--accent) 14%,var(--surface))}.nullable[data-step="2"] .null-empty{opacity:.2;transform:scale(.8)}.nullable[data-step="2"] .null-value{transform:translateY(-5px)}
js
const root=document.querySelector('.visual'),cap=root.querySelector('.visual-caption'),dots=[...root.querySelectorAll('.visual-dots b')],captions=["Value may be absent","Null path stops","Value path permits access"];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()})

strict null 검사에서는 값 T와 null을 서로 다른 가능성으로 취급합니다. T | null인 값을 곧바로 T처럼 사용하면 타입 검사에서 막힙니다.

데모는 값 경로와 null 경로를 갈라 보여주고, 검사 뒤 값 경로에서만 메서드 호출을 엽니다. non-null 단언으로 검사를 건너뛰면 런타임 위험을 떠안습니다.

언제 쓰나

조회 결과나 선택 입력처럼 값 부재가 정상 상태일 때 사용합니다.

페이지로 열기 ↗