Skip to content
Laviyx Learn

Try

TypeScript Basics Practice

Practise type annotations, arrays and tuples, typed functions, object types and optional properties. Every solution has been type-checked in strict mode and run, and shows its real output.

Beginner6 problemsUpdated

Review first: Basic Types and Annotations, Arrays and Tuples, Typing Functions, Object Types, null and undefined

  1. 1.A typed price function

    Beginner

    Write total(price: number, quantity: number, discountPercent = 0): number and print the total for 3 items at ₹250 with no discount, and with a 10% discount.

    Show hint

    The default value makes discountPercent optional and infers it as a number.

    Show solution
    TypeScript
    function total(price: number, quantity: number, discountPercent = 0): number {
      const subtotal = price * quantity;
      return subtotal - (subtotal * discountPercent) / 100;
    }
    console.log(total(250, 3), total(250, 3, 10));
    Output
    750 675
  2. 2.An object type with optional properties

    Beginner

    Define a Product type with name, price, an optional discountPercent and a readonly sku. Write finalPrice(p: Product): number that applies the discount if there is one, and test it with and without a discount.

    Show hint

    Use p.discountPercent ?? 0 to handle the missing value.

    Show solution
    TypeScript
    type Product = {
      readonly sku: string;
      name: string;
      price: number;
      discountPercent?: number;
    };
    
    function finalPrice(p: Product): number {
      return p.price * (1 - (p.discountPercent ?? 0) / 100);
    }
    
    const pen: Product = { sku: "P-1", name: "Pen", price: 20 };
    const bag: Product = { sku: "B-7", name: "Bag", price: 1500, discountPercent: 20 };
    console.log(finalPrice(pen), finalPrice(bag));
    Output
    20 1200
  3. 3.Return a tuple

    Beginner

    Write minMax(values: readonly number[]): [min: number, max: number] and destructure its result for [7, 3, 9, 4].

    Show hint

    Math.min(...values) and Math.max(...values).

    Show solution
    TypeScript
    function minMax(values: readonly number[]): [min: number, max: number] {
      return [Math.min(...values), Math.max(...values)];
    }
    const [low, high] = minMax([7, 3, 9, 4]);
    console.log(low, high);
    Output
    3 9
  4. 4.Handling a possibly missing value

    Beginner

    Write fullName(first: string, last: string, middle?: string): string that includes the middle name only when it is given. Print the result with and without a middle name.

    Show hint

    middle has the type string | undefined; check it before using it.

    Show solution
    TypeScript
    function fullName(first: string, last: string, middle?: string): string {
      return middle ? `${first} ${middle} ${last}` : `${first} ${last}`;
    }
    console.log(fullName("Asha", "Rao"));
    console.log(fullName("Asha", "Rao", "Devi"));
    Output
    Asha Rao
    Asha Devi Rao
  5. 5.A typed callback parameter

    Intermediate

    Write countWhere(items: number[], test: (n: number) => boolean): number and use it to count the even numbers and the numbers above 50 in [12, 55, 8, 91, 40].

    Show hint

    The callback's type is written with arrow syntax.

    Show solution
    TypeScript
    function countWhere(items: number[], test: (n: number) => boolean): number {
      let count = 0;
      for (const n of items) if (test(n)) count++;
      return count;
    }
    const values = [12, 55, 8, 91, 40];
    console.log(countWhere(values, (n) => n % 2 === 0), countWhere(values, (n) => n > 50));
    Output
    3 2
  6. 6.A safe dictionary lookup

    Intermediate

    Given const prices: Record<string, number> = { tea: 20, coffee: 35 }, write priceOf(item: string): string that returns "tea: ₹20" style text, or "juice: not available" if the item is missing.

    Show hint

    Check item in prices (or compare with undefined) before using the value.

    Show solution
    TypeScript
    const prices: Record<string, number> = { tea: 20, coffee: 35 };
    
    function priceOf(item: string): string {
      const price = prices[item];
      return price === undefined ? `${item}: not available` : `${item}: ₹${price}`;
    }
    console.log(priceOf("tea"), "|", priceOf("juice"));
    Output
    tea: ₹20 | juice: not available