Skip to content
FrontDepth, Home

Back to the topic

Practice: Structural typing and type erasure

Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.

4 predict-output · 4 open

Questions up to level

Shows questions up to the selected level.

No practice questions for this level yet.

  1. Predict the outputJunior

    The result of JSON.parse is annotated as a Product, and the code type-checks under --strict. What does it log?

    TypeScript
    interface Product {
      title: string;
      price: number;
    }
    
    const body = '{"title":"Kettle","price":"25"}';
    const product: Product = JSON.parse(body);
    
    console.log(typeof product.price);
    console.log(product.price + 5);
    console.log(product.price * 2);
  2. Predict the outputJunior

    describe takes a Dog. All three calls type-check under --strict. What does it log?

    TypeScript
    class Dog {
      name = "Rex";
      speak() {
        return "woof";
      }
    }
    
    class Robot {
      name = "R2";
      speak() {
        return "beep";
      }
    }
    
    function describe(dog: Dog): string {
      return `${dog.name} ${dog.speak()} ${dog instanceof Dog}`;
    }
    
    console.log(describe(new Dog()));
    console.log(describe(new Robot()));
    console.log(describe({ name: "Toy", speak: () => "squeak" }));
  3. Open questionJunior

    What does it mean that TypeScript's types are erased, and what follows from it?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  4. Predict the outputMiddle

    toPublic and toPublicPicked both promise a PublicUser, and both type-check under --strict. What does it log?

    TypeScript
    interface User {
      id: number;
      name: string;
      passwordHash: string;
    }
    
    interface PublicUser {
      id: number;
      name: string;
    }
    
    function toPublic(user: User): PublicUser {
      return user;
    }
    
    function toPublicPicked(user: User): PublicUser {
      return { id: user.id, name: user.name };
    }
    
    const user: User = { id: 7, name: "Ada", passwordHash: "x9f2" };
    
    console.log(JSON.stringify(toPublic(user)));
    console.log(JSON.stringify(toPublicPicked(user)));
    console.log(Object.keys(toPublic(user)).length);
  5. Open questionMiddle

    What is structural typing, and why does TypeScript sometimes reject an object that has extra properties?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  6. Open questionMiddle

    What is the difference between an interface and a type alias, and how do you choose between them?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  7. Predict the outputSenior

    UserId is a branded string. The code type-checks under --strict. What does it log?

    TypeScript
    type UserId = string & { readonly __brand: "UserId" };
    
    function toUserId(raw: string): UserId {
      if (!raw.startsWith("u_")) throw new Error("bad id");
      return raw as UserId;
    }
    
    const id = toUserId("u_42");
    
    console.log(typeof id);
    console.log(id.length);
    console.log(JSON.stringify({ id }));
    console.log("__brand" in Object(id));
  8. Open questionSenior

    TypeScript is structurally typed. How do you stop a UserId from being passed where an OrderId is expected, and what does the technique cost?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.