Technology Encyclopedia Home >What is the basic syntax of TypeScript?

What is the basic syntax of TypeScript?

TypeScript is a superset of JavaScript that adds optional static typing and other features to the language. Its basic syntax is similar to JavaScript, with additional type annotations and other type-related features.

Here is a simple example of TypeScript code:

// Define a variable with a specific type
let myNumber: number = 10;

// Define a function with type annotations
function addNumbers(a: number, b: number): number {
    return a + b;
}

// Call the function and store the result in a variable
let result: number = addNumbers(5, 7);

// Output the result
console.log(result); // Outputs: 12

In this example:

  • let myNumber: number = 10; declares a variable myNumber of type number.
  • function addNumbers(a: number, b: number): number defines a function addNumbers that takes two parameters of type number and returns a value of type number.
  • let result: number = addNumbers(5, 7); calls the function and stores the result in a variable result of type number.

TypeScript also supports more advanced features like interfaces, classes, and generics, which help in building scalable and maintainable applications.

For cloud-based development and deployment, you might consider using services like Tencent Cloud, which offer robust support for TypeScript through their various platforms and tools.