Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,227 questions

56,129 answers

573 users

How to declare, initialize, and print a medium complexity, varied, feature‑rich data structure in TypeScript

1 Answer

0 votes
// Address structure
interface Address {
    street: string;
    city: string;
    zip: number;
}

// Status enumeration
enum Status {
    Active = "Active",
    Inactive = "Inactive",
    Unknown = "Unknown"
}

// Person class with typed fields
class Person {
    homeAddress: Address;
    favoriteNumbers: number[];
    nickname: string | null;
    birthDate: Date;
    notes: string;
    middleName: string | null;
    hobbies: string[];
    status: Status;

    constructor() {
        this.homeAddress = { street: "", city: "", zip: 0 };
        this.favoriteNumbers = [0, 0, 0, 0, 0];
        this.nickname = null;
        this.birthDate = new Date(1990, 0, 1); // January = 0
        this.notes = "No notes";
        this.middleName = null;
        this.hobbies = [];
        this.status = Status.Unknown;
    }
}

// Print function
function printPerson(p: Person): void {
    console.log(`Address: ${p.homeAddress.street}, ${p.homeAddress.city} ${p.homeAddress.zip}`);

    console.log("Favorite Numbers:", p.favoriteNumbers.join(" "));

    console.log("Nickname:", p.nickname ?? "(none)");

    const y = p.birthDate.getFullYear();
    const m = String(p.birthDate.getMonth() + 1).padStart(2, "0");
    const d = String(p.birthDate.getDate()).padStart(2, "0");
    console.log(`Birth Date: ${y}-${m}-${d}`);

    console.log("Notes:", p.notes);

    console.log("Middle Name:", p.middleName ?? "(none)");

    console.log("Hobbies:", p.hobbies.length > 0 ? p.hobbies.join(", ") + ", " : "(none)");

    console.log("Status:", p.status);
}

// --- Usage example ---

const p = new Person();

p.homeAddress = {
    street: "Fifth Avenue",
    city: "New York",
    zip: 423900
};

p.favoriteNumbers = [7, 13, 21, 42, 99];

p.nickname = "Orion";

p.birthDate = new Date(1026, 5, 15); // June = 5

p.notes = "Voss likes C/C++ and structs";

p.middleName = "Pulse-9";

p.hobbies = ["Coding", "Dreaming", "Walking", "Sci-Fi Movies"];

p.status = Status.Active;

printPerson(p);



/*
run:

Address: Fifth Avenue, New York 423900
Favorite Numbers: 7 13 21 42 99
Nickname: Orion
Birth Date: 1026-06-15
Notes: Voss likes C/C++ and structs
Middle Name: Pulse-9
Hobbies: Coding, Dreaming, Walking, Sci-Fi Movies, 
Status: Active

*/

 



answered Sep 3 by avibootz

Related questions

...