Master TypeScript's powerful type system through hands-on coding challenges. Build type-safe applications with confidence.
Write real TypeScript code, learn type annotations, and build type-safe applications.
username with explicit type string. In TypeScript, you add a colon after the variable name followed by the type: let username: string = "value";userAge with type number. Unlike JavaScript, TypeScript has a single number type for all numeric values (integers, floats, etc.).isActive with type boolean. This can only hold true or false.hobbies that holds strings. There are two syntaxes: string[] or Array<string>. We recommend string[] for simplicity.userInfo as a tuple containing exactly [string, number] - the user's name and age. Tuples are fixed-length arrays where each position has a specific type: let userInfo: [string, number] = ["Alice", 30];Username: Alice (type: string)
Age: 28 (type: number)
Active: true (type: boolean)
Hobbies: coding,gaming,reading
User Info Tuple: Alice, 28
let username: string = "Alice";let userAge: number = 28;let isActive: boolean = true;let hobbies: string[] = ["coding", "gaming"];let userInfo: [string, number] = ["Alice", 28];console.log() just like JavaScriptReview feedback below
User with these properties:id: number - Unique identifiername: string - User's full nameemail: string - User's email addressisAdmin: boolean - Whether user has admin privilegesphone?: string property. The ? makes it optional - objects can omit this property without causing errors.readonly createdAt: string property. Readonly properties cannot be modified after the object is created.user1 of type User and assign it an object with all required properties.user2 without the phone property to demonstrate optional properties work.interface InterfaceName {
property1: type;
property2: type;
optionalProp?: type;
readonly readonlyProp: type;
}User 1: John Doe (john@example.com)
Admin: true, Phone: 555-1234
User 2: Jane Smith (jane@example.com)
Admin: false, Phone: Not provided
interface User { id: number; name: string; }phone?: string; (note the ?)readonly createdAt: string;let user1: User = { id: 1, name: "John", ... };user1.phone || "Not provided"Review feedback below
name: string parameter and returns a string. The return type goes after the parameter list:function greet(name: string): string {
return "Hello, " + name + "!";
}a: number and b: number, returns a number. This demonstrates multiple typed parameters.createUser that takes name: string and optional age?: number. Return a formatted string with the user info.multiply that takes a: number and b: number = 2 (default value). If b is not provided, it defaults to 2.isEven that takes a number and returns a boolean:const isEven = (num: number): boolean => num % 2 === 0;function name(param: type): returnType { }const name = (param: type): returnType => expression;void when function doesn't return anythingGreeting: Hello, TypeScript!
Sum: 15
User: Alice, Age: 25
User: Bob, Age: not specified
Multiply 5 * 2: 10
Multiply 5 * 3: 15
Is 4 even? true
Is 7 even? false
function name(param: type): returnType { return value; }function fn(name: string, age?: number): stringfunction multiply(a: number, b: number = 2): numberconst isEven = (n: number): boolean => n % 2 === 0;age !== undefined ? age : "not specified"Review feedback below