TypeScript
Simply put, TypeScript is JavaScript with types.
JavaScript is a dynamically typed language, meaning that variable types are determined at runtime. TypeScript is a superset of JavaScript that introduces static typing. Developers can specify variable types during development, and the TypeScript compiler enforces these types.
// JavaScript code
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("John")); // Outputs: Hello, John!
// TypeScript code
function greet(name: string): string {
return "Hello, " + name + "!";
}
console.log(greet("John")); // Outputs: Hello, John!
TypeScript allows developers to annotate variables, function parameters, and return types with specific types, providing better tooling and catching potential errors early in the development process.
TypeScript has additional features such as interfaces, which help developers structure and organize their code in an object-oriented manner.
// JavaScript code
function printPerson(person) {
console.log("Name: " + person.name + ", Age: " + person.age);
}
const john = { name: "John", age: 30 };
printPerson(john);
// TypeScript code
interface Person {
name: string;
age: number;
}
function printPerson(person: Person): void {
console.log(`Name: ${person.name}, Age: ${person.age}`);
}
const john: Person = { name: "John", age: 30 };
printPerson(john);
Keep Going. Keep Learning!
Loading...
> code result goes here