Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara 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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 5.56

bookWriting Your First TypeScript Code

Scorri per mostrare il menu

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 4
some-alt