TypeScript is a programming language that is a superset of JavaScript. What is its main purpose?
Think about what TypeScript adds to JavaScript to help developers.
TypeScript adds static typing to JavaScript, which helps catch errors early during development.
Consider this TypeScript code snippet:
function greet(name: string) {
return `Hello, ${name.toUpperCase()}!`;
}
console.log(greet("world"));What will it print?
function greet(name: string) { return `Hello, ${name.toUpperCase()}!`; } console.log(greet("world"));
Look at how the toUpperCase() method changes the string.
The function converts the input string to uppercase, so "world" becomes "WORLD".
Look at this TypeScript code:
let age: number = "30";
What error will the TypeScript compiler show?
let age: number = "30";
Check the type declared and the value assigned.
TypeScript expects a number but got a string, so it reports a type mismatch error.
Which of these features is only available in TypeScript and not in plain JavaScript?
Think about features that help describe data structures before running code.
Interfaces are a TypeScript feature to describe object shapes and types, not present in JavaScript.
Analyze this TypeScript code:
type User = { name: string; age: number };
const users: User[] = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Carol", age: 35 }
];
const result = users.filter(u => u.age > 28).map(u => u.name).join(", ");
console.log(result);What will be printed?
type User = { name: string; age: number };
const users: User[] = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Carol", age: 35 }
];
const result = users.filter(u => u.age > 28).map(u => u.name).join(", ");
console.log(result);Look at the filter condition and what is extracted after filtering.
The code filters users older than 28, which are Bob and Carol, then joins their names with a comma.