tsc compiler
- TypeScript course
- Beginner
- Lesson 3/89
- 4 min
Learn to convert .ts files to .js with the TypeScript compiler tsc.
The tsc compiler
tsc is the TypeScript compiler. It takes a .ts file, checks the types, and produces plain JavaScript by stripping type annotations. By default it creates a .js file with the same name.
tsc selam.ts
# selam.js is createdYou can then run the output in Node or the browser.
The --noEmit flag
By default tsc produces JavaScript. With the --noEmit flag it only checks types and creates no file. This is common in build tools (e.g. Vite).
tsc --noEmit selam.ts
# only shows errors, no .js is createdPractice A function to compile
Write a function named selamVer. Its parameter is ad: string, its return type is string. Its body returns "Merhaba, " + ad. Then print the result of selamVer("Dünya") with console.log.
Starter code
// selamVer fonksiyonunu tanımla
// parametre: ad: string, dönüş: string
// selamVer("Dünya") sonucunu yazdırSample solution
function selamVer(ad: string): string {
return "Merhaba, " + ad;
}
console.log(selamVer("Dünya"));Expected output
Merhaba, Dünya
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does tsc do by default?
- Turns the .js file into .ts
- Only checks types, creates no file
- Deletes the .ts file
- Turns the .ts file into .js Correct answer
Explanation: By default tsc compiles the .ts file and writes a .js file with the same name. The 'check only' behavior requires the --noEmit flag.
2 Output extension
// tsc app.ts çalışınca app.____ oluşur
// tsc app.ts çalışınca app.js oluşur
Explanation: By default tsc produces a .js file with the same name from the .ts file. .ts is the source, .js is the output.
3 tsc parts
| Term | Meaning |
|---|---|
tsc |
TypeScript compiler |
.ts |
Source file |
.js |
Output file |
--noEmit |
Creates no file |