Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте 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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 4

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

What are type annotations in TypeScript?

How does TypeScript help prevent errors?

Can I use TypeScript with existing JavaScript projects?

Awesome!

Completion rate improved to 5.56

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 4
some-alt