π Create a file called example.ts
// example.ts
function greet(name: string): string {
return `Hello, ${name}`;
}
const message = greet("TypeScript");
console.log(message);β Notes:
The function
greetexpects astringargument and returns astring.TypeScript will check that
nameis a string during compile time.
β‘ Compiling the TypeScript file
π Open your terminal and run:
tsc example.tsβ This will generate a file:
example.jsπ The output JavaScript will look like:
function greet(name) {
return "Hello, " + name;
}
const message = greet("TypeScript");
console.log(message);β‘ Notice the types are gone! TypeScript strips types because JavaScript doesnβt support them β but the type checks happen during compilation.
βοΈ Running the compiled JavaScript
Run your JS file using Node (or in a browser):
node example.jsπ Optional: Watch mode
If you want TypeScript to compile automatically whenever you save your file:
tsc example.ts --watchNow every time you save example.ts, it re-compiles automatically.
π Using tsconfig.json for multi-file projects
If you have multiple .ts files:
1οΈβ£ Generate a config file:
tsc --init2οΈβ£ Edit tsconfig.json (optional, e.g., set "outDir": "./dist")
3οΈβ£ Compile the whole project:
tscβ
All TypeScript files in the project will be compiled according to tsconfig.json rules.
β¨ Summary of steps
Step | Command |
|---|---|
Write | (e.g., |
Compile |
|
Run JS |
|
Watch mode |
|
Multi-file project |
|
<div class="infographic-ts" id="infographic-ts">
<style>
.infographic-ts {
--body-font: system-ui, -apple-system, sans-serif;
--mono-font: ui-monospace, SFMono-Regular, Consolas, "Courier New", monospace;
--ed-bg: #1e1e1e;
--ed-bar: #2a2a2a;
--ed-text: #d4d4d4;
--ed-muted: #8a8a8a;
--ed-prompt: #4ec9b0;
--ed-out: #d4d4d4;
--ed-ok: #4ec9b0;
--run-bg: #2e7d46;
--run-bg-hover: #35924f;
--tok-kw: #c586c0;
--tok-type: #4ec9b0;
--tok-str: #ce9178;
--tok-fn: #dcdcaa;
--tok-punc: #d4d4d4;
--arrow: #8a8a8a;
max-width: 640px;
margin: 0 auto;
box-sizing: border-box;
background: transparent;
font-family: var(--body-font);
}
.infographic-ts * { box-sizing: border-box; }
.code-window { background: var(--ed-bg); border-radius: 8px; overflow: hidden;
transition: opacity 0.4s ease, max-height 0.4s ease; }
.code-window.hidden { opacity: 0; max-height: 0; }
.code-window.revealed { opacity: 1; max-height: 400px; }
.code-topbar { display: flex; align-items: center; justify-content: space-between;
padding: 0.55rem 0.8rem; background: var(--ed-bar); }
.code-dots { display: flex; gap: 6px; }
.code-dot { width: 10px; height: 10px; border-radius: 50%; }
.code-dot.r { background: #ff5f57; }
.code-dot.y { background: #febc2e; }
.code-dot.g { background: #28c840; }
.code-filename { font-family: var(--mono-font); font-size: 0.76rem; color: var(--ed-muted); }
.run-btn { display: inline-flex; align-items: center; gap: 0.35rem; background: var(--run-bg);
color: #fff; border: none; border-radius: 4px; padding: 0.35rem 0.75rem; font-size: 0.76rem;
font-family: var(--body-font); font-weight: 500; cursor: pointer; }
.run-btn:hover:not(:disabled) { background: var(--run-bg-hover); }
.run-btn:disabled { opacity: 0.4; cursor: default; }
.run-btn svg { width: 11px; height: 11px; }
.code-body { padding: 0.9rem 1.25rem; font-family: var(--mono-font); font-size: 0.82rem;
line-height: 1.6; color: var(--ed-out); white-space: pre; overflow-x: auto; }
.prompt { color: var(--ed-prompt); }
.tok-kw { color: var(--tok-kw); }
.tok-type { color: var(--tok-type); }
.tok-str { color: var(--tok-str); }
.tok-fn { color: var(--tok-fn); }
.tok-punc { color: var(--tok-punc); }
.console { padding: 0 1.25rem; max-height: 0; overflow: hidden;
font-family: var(--mono-font); font-size: 0.8rem; line-height: 1.6;
transition: max-height 0.35s ease, padding 0.35s ease; }
.console.show { max-height: 140px; padding: 0 1.25rem 1rem; }
.console-line { color: var(--ed-out); }
.console-line.ok { color: var(--ed-ok); }
.flow-arrow { display: flex; justify-content: center; margin: 0.4rem 0; }
.flow-arrow svg { width: 18px; height: 18px; color: var(--arrow); }
.ts-header { font-family: var(--body-font); font-weight: 600; font-size: 1.4rem;
color: inherit; text-align: center; margin: 0 0 1.25rem; }
</style>
<h2 class="ts-header">Writing and compiling TypeScript files</h2>
<div class="code-window" id="w1">
<div class="code-topbar">
<div class="code-dots"><span class="code-dot r"></span><span class="code-dot y"></span><span class="code-dot g"></span></div>
<span class="code-filename">example.ts</span>
</div>
<div class="code-body"><span class="tok-kw">function</span> <span class="tok-fn">greet</span><span class="tok-punc">(name: </span><span class="tok-type">string</span><span class="tok-punc">): </span><span class="tok-type">string</span> <span class="tok-punc">{</span>
<span class="tok-kw">return</span> <span class="tok-str">`Hello, ${</span>name<span class="tok-str">}`</span><span class="tok-punc">;</span>
<span class="tok-punc">}</span>
<span class="tok-kw">const</span> message = <span class="tok-fn">greet</span><span class="tok-punc">(</span><span class="tok-str">"TypeScript"</span><span class="tok-punc">);</span>
console.<span class="tok-fn">log</span><span class="tok-punc">(message);</span></div>
</div>
<div class="flow-arrow">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 5v14"/><path d="m19 12-7 7-7-7"/></svg>
</div>
<div class="code-window" id="w2">
<div class="code-topbar">
<div class="code-dots"><span class="code-dot r"></span><span class="code-dot y"></span><span class="code-dot g"></span></div>
<span class="code-filename">terminal</span>
<button class="run-btn" id="compileBtn" type="button">
<svg viewBox="0 0 24 24" fill="currentColor"><polygon points="6 3 20 12 6 21 6 3"/></svg>
run
</button>
</div>
<div class="code-body"><span class="prompt">$</span> tsc example.ts</div>
<div class="console" id="compileConsole"></div>
</div>
<div class="flow-arrow">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 5v14"/><path d="m19 12-7 7-7-7"/></svg>
</div>
<div class="code-window hidden" id="w3">
<div class="code-topbar">
<div class="code-dots"><span class="code-dot r"></span><span class="code-dot y"></span><span class="code-dot g"></span></div>
<span class="code-filename">example.js</span>
</div>
<div class="code-body"><span class="tok-kw">function</span> <span class="tok-fn">greet</span><span class="tok-punc">(name) {</span>
<span class="tok-kw">return</span> <span class="tok-str">"Hello, "</span> + name<span class="tok-punc">;</span>
<span class="tok-punc">}</span>
<span class="tok-kw">const</span> message = <span class="tok-fn">greet</span><span class="tok-punc">(</span><span class="tok-str">"TypeScript"</span><span class="tok-punc">);</span>
console.<span class="tok-fn">log</span><span class="tok-punc">(message);</span></div>
</div>
<div class="flow-arrow">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 5v14"/><path d="m19 12-7 7-7-7"/></svg>
</div>
<div class="code-window" id="w4">
<div class="code-topbar">
<div class="code-dots"><span class="code-dot r"></span><span class="code-dot y"></span><span class="code-dot g"></span></div>
<span class="code-filename">terminal</span>
<button class="run-btn" id="runBtn" type="button" disabled>
<svg viewBox="0 0 24 24" fill="currentColor"><polygon points="6 3 20 12 6 21 6 3"/></svg>
run
</button>
</div>
<div class="code-body"><span class="prompt">$</span> node example.js</div>
<div class="console" id="runConsole"></div>
</div>
<script>
(function () {
var wrapper = document.currentScript.closest('.infographic-ts')
|| document.querySelector('.infographic-ts');
var root = wrapper;
function line(consoleEl, cls, text) {
var el = document.createElement('div');
el.className = 'console-line ' + (cls || '');
el.textContent = text;
consoleEl.appendChild(el);
}
var compileBtn = root.querySelector('#compileBtn');
var compileConsole = root.querySelector('#compileConsole');
var w3 = root.querySelector('#w3');
var runBtn = root.querySelector('#runBtn');
compileBtn.addEventListener('click', function () {
compileBtn.disabled = true;
compileConsole.innerHTML = '';
compileConsole.classList.add('show');
line(compileConsole, '', 'Checking types\u2026');
setTimeout(function () {
line(compileConsole, 'ok', '\u2713 example.js written \u2014 no type errors');
w3.classList.remove('hidden');
w3.classList.add('revealed');
runBtn.disabled = false;
compileBtn.disabled = false;
}, 700);
});
var runConsole = root.querySelector('#runConsole');
runBtn.addEventListener('click', function () {
runBtn.disabled = true;
runConsole.innerHTML = '';
runConsole.classList.add('show');
setTimeout(function () {
line(runConsole, 'ok', 'Hello, TypeScript');
runBtn.disabled = false;
}, 400);
});
})();
</script>
</div>