Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Writing Your First TypeScript Code | Introduction to TypeScript
TypeScript Foundations

bookWriting Your First TypeScript Code

When you write a TypeScript file, you are creating a script with a .ts extension that closely resembles JavaScript, but with additional features for static type checking. The structure of a TypeScript file is straightforward: you write your code just as you would in JavaScript, but you can also add type annotations to variables, function parameters, and return values. This makes your code more readable and helps prevent errors by catching type mismatches before the code runs. TypeScript files are designed to integrate seamlessly with existing JavaScript code, so you can gradually add TypeScript to any JavaScript project.

12345678910
// A simple TypeScript function with type annotations function greet(name: string): string { return "Hello, " + name + "!"; } // This will work: console.log(greet("Alice")); // This will cause a TypeScript error if uncommented, because the argument is not a string: // console.log(greet(42));
copy

After writing your TypeScript code, you need to convert it into JavaScript so that browsers or Node.js can execute it. This conversion is called compilation. You use the TypeScript compiler (tsc) to transform your .ts files into .js files. The compiler checks your code for type errors and only produces JavaScript output if your code passes all type checks. The resulting JavaScript file contains the same logic as your TypeScript code but without any type annotations, making it compatible with any environment that runs JavaScript.

question mark

Which statement best describes what happens when you compile a TypeScript file?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 4

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 5.56

bookWriting Your First TypeScript Code

Sveip for å vise menyen

When you write a TypeScript file, you are creating a script with a .ts extension that closely resembles JavaScript, but with additional features for static type checking. The structure of a TypeScript file is straightforward: you write your code just as you would in JavaScript, but you can also add type annotations to variables, function parameters, and return values. This makes your code more readable and helps prevent errors by catching type mismatches before the code runs. TypeScript files are designed to integrate seamlessly with existing JavaScript code, so you can gradually add TypeScript to any JavaScript project.

12345678910
// A simple TypeScript function with type annotations function greet(name: string): string { return "Hello, " + name + "!"; } // This will work: console.log(greet("Alice")); // This will cause a TypeScript error if uncommented, because the argument is not a string: // console.log(greet(42));
copy

After writing your TypeScript code, you need to convert it into JavaScript so that browsers or Node.js can execute it. This conversion is called compilation. You use the TypeScript compiler (tsc) to transform your .ts files into .js files. The compiler checks your code for type errors and only produces JavaScript output if your code passes all type checks. The resulting JavaScript file contains the same logic as your TypeScript code but without any type annotations, making it compatible with any environment that runs JavaScript.

question mark

Which statement best describes what happens when you compile a TypeScript file?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 4
some-alt