What is TypeScript?
- TypeScript course
- Beginner
- Lesson 1/89
- 4 min
Learn that TypeScript is a typed superset of JavaScript.
What is TypeScript?
TypeScript is a typed superset of JavaScript. That is, every valid JavaScript program is also a valid TypeScript program. TypeScript source files use the .ts extension.
let isim: string = "Ayşe";
let yas: number = 30;Type annotations are checked only at compile time; the output is still plain JavaScript.
Type annotation syntax
In TypeScript you specify a variable's type after its name with a colon. If you try to assign a wrong type, the compiler warns you.
let puan: number = 95;
puan = "yüksek"; // Error: 'string' is not assignable to 'number'Type annotations are optional; in most cases TypeScript infers the type from the assigned value.
Practice Your first type annotation
Declare a variable named yas. Its type is number, its value is 25. Then print the yas variable with console.log.
Starter code
// yas değişkenini number türüyle tanımla
// değer 25 olsun
// console.log ile yazdırSample solution
let yas: number = 25;
console.log(yas);Expected output
25
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does it mean that TypeScript is a 'superset' of JavaScript?
- Every valid JavaScript program is also valid TypeScript. Correct answer
- Every valid TypeScript program is also valid JavaScript.
- TypeScript completely replaces JavaScript.
- There is no relationship between TypeScript and JavaScript.
Explanation: A superset includes all of JavaScript's syntax and adds types on top. So every JS code is valid in TS; however TS code with type annotations does not run directly as JS.
2 TypeScript file extension
// Kaynak dosya: selam.____
let mesaj: string = "Merhaba";
// Kaynak dosya: selam.ts
let mesaj: string = "Merhaba";
Explanation: TypeScript source files use the .ts extension. .js is the compiled JavaScript output.
3 TypeScript core concepts
| Term | Meaning |
|---|---|
: string |
Type annotation |
.ts |
Source file extension |
üst küme |
JS code is valid in TS |
JavaScript |
TypeScript's foundation |