Writing 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));
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.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
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
Writing Your First TypeScript Code
Swipe to show 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));
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.
Thanks for your feedback!