TypeScript 線上演練場
在瀏覽器中編寫、編譯、進行型別檢查並執行 TypeScript。支援即時診斷、JavaScript 輸出、嚴格模式控制、快速範例以及沙盒主控台。
在瀏覽器中編寫、編譯、進行型別檢查並執行 TypeScript。支援即時診斷、JavaScript 輸出、嚴格模式控制、快速範例以及沙盒主控台。
'; updateStage('run', false); runnerEl.srcdoc = sandboxHtml; } function downloadJavaScript() { const blob = new Blob([jsOutputEl.value], { type: 'text/javascript;charset=utf-8' }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = 'playground.js'; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); } function tidyIndents() { const tidied = sourceEl.value .replace(/\t/g, ' ') .split('\n') .map(function (line) { return line.replace(/\s+$/g, ''); }) .join('\n') .replace(/\n{4,}/g, '\n\n\n'); sourceEl.value = tidied; compileNow(); } function resetSource() { sourceEl.value = document.getElementById('tsplay-source').defaultValue; compileNow(); } sourceEl.addEventListener('input', scheduleCompile); sourceEl.addEventListener('scroll', function () { linesEl.scrollTop = sourceEl.scrollTop; }); sourceEl.addEventListener('keydown', function (event) { if (event.key === 'Tab') { event.preventDefault(); const start = sourceEl.selectionStart; const end = sourceEl.selectionEnd; sourceEl.value = sourceEl.value.slice(0, start) + ' ' + sourceEl.value.slice(end); sourceEl.selectionStart = sourceEl.selectionEnd = start + 2; scheduleCompile(); } }); document.querySelectorAll('[data-example]').forEach(function (button) { button.addEventListener('click', function () { const name = button.getAttribute('data-example'); if (EXAMPLES[name]) { sourceEl.value = EXAMPLES[name]; compileNow(); } }); }); document.querySelectorAll('.tsplay-result .tsplay-tab').forEach(function (tab) { tab.addEventListener('click', function () { setActiveTab(tab.getAttribute('data-tab')); }); }); [targetEl, moduleEl, strictEl, indexedEl, declarationsEl].forEach(function (control) { control.addEventListener('change', compileNow); }); compileBtn.addEventListener('click', compileNow); runBtn.addEventListener('click', runJavaScript); document.getElementById('tsplay-copy-source').addEventListener('click', function () { copyText(sourceEl.value, 'TypeScript'); }); document.getElementById('tsplay-copy-js').addEventListener('click', function () { copyText(jsOutputEl.value, 'JavaScript'); }); document.getElementById('tsplay-download-js').addEventListener('click', downloadJavaScript); document.getElementById('tsplay-format-btn').addEventListener('click', tidyIndents); document.getElementById('tsplay-clear-console').addEventListener('click', function () { consoleEl.textContent = '執行編譯後的 JavaScript 即可在此處查看主控台輸出。'; }); document.getElementById('tsplay-clear-btn').addEventListener('click', resetSource); window.addEventListener('message', function (event) { if (!event.data || event.data.tsplayToken !== runnerToken) { return; } addConsoleLine(event.data.kind, event.data.value); }); function waitForCompiler(attempt) { syncEditorStats(); if (window.ts) { compilerStateEl.textContent = 'TypeScript ' + window.ts.version + ' 就緒'; setStatus('ready', '編譯器就緒'); compileNow(); return; } if (attempt > 80) { compilerStateEl.textContent = '編譯器無法使用'; setStatus('error', '編譯器無法使用'); diagnosticsEl.innerHTML = '
'; return; } setTimeout(function () { waitForCompiler(attempt + 1); }, 150); } waitForCompiler(0); })();
你的廣告攔截器讓我們無法顯示廣告
MiniWebtool 依靠廣告收入免費提供服務。如果這個工具幫到了你,歡迎升級以獲得無廣告瀏覽和更多每日使用次數,或將 MiniWebtool.com 加入白名單後重新整理。
此 TypeScript Playground 讓您無需開啟完整專案即可快速測試程式片段。它在您的瀏覽器中執行 TypeScript 編譯器,顯示附行號的型別檢查診斷資訊,輸出 JavaScript,並可在沙箱主控台中執行簡單輸出。
此工具是為日常 TypeScript 檢查而設計的:驗證聯合型別縮小、測試泛型輔助型別、比較編譯器目標、確認 strict 模式行為,以及檢視 TypeScript 輸出的 JavaScript。它適用於學習、除錯精簡範例、準備解答,或在將小型重構移入專案前進行檢查。
瀏覽器編譯器對常見的 JavaScript 物件(如陣列、字串、記錄、JSON、console 及計時器)使用精簡的內建宣告檔案。這讓 playground 在處理程式片段時保持快速且自給自足。如果您的程式碼依賴 React、Node.js、DOM API 或特定套件的宣告,請在您的專案中使用實際的 tsconfig.json 與已安裝的型別套件來確認最終結果。
當您想從較大的程式碼庫中隔離型別錯誤、比較 strict 模式如何改變診斷、向團隊成員解釋 TypeScript 概念,或檢視小型程式片段如何編譯為 JavaScript 時,請使用此 playground。內建範例涵蓋可區別聯合、泛型記錄輔助函式、satisfies 運算子以及刻意的型別錯誤,讓您可以同時看到正常與失敗的編譯器狀態。
會。它會在瀏覽器中建置 TypeScript 程式,並回報來自編譯器的診斷資訊,包括型別錯誤、語法錯誤以及編譯器選項訊息。
可以。主要控制項涵蓋 JavaScript 目標、模組輸出、strict 模式、宣告檔輸出以及 noUncheckedIndexedAccess。
此 playground 刻意使用精簡的內建型別函式庫。對於框架、DOM、Node.js 或 npm 套件的型別,請使用您本機的專案設定。
此執行器在快速程式片段時使用沙箱化的 iframe。請避免執行不受信任的程式碼,並在您一般的開發環境中測試真實的應用程式碼。
引用此內容、頁面或工具為:
"TypeScript 線上演練場" 於 https://MiniWebtool.com/zh-tw/typescript-線上演練場/,來自 MiniWebtool,https://MiniWebtool.com/
by miniwebtool team. Updated: 2026-05-23