Challenge - 5 Problems
TypeScript Assignment Compatibility Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of assignment compatibility with interfaces
What is the output of this TypeScript code when assigning objects to interface types?
Typescript
interface Person { name: string; age: number; }
interface Employee { name: string; age: number; employeeId: string; }
const p: Person = { name: "Alice", age: 30 };
const e: Employee = { name: "Bob", age: 40, employeeId: "E123" };
const p2: Person = e;
// const e2: Employee = p; // Uncommenting this line causes error
console.log(p2.name);Attempts:
2 left
💡 Hint
Think about whether a type with more properties can be assigned to a type with fewer properties.
✗ Incorrect
In TypeScript, an object with extra properties can be assigned to a type requiring fewer properties (structural typing). So, Employee can be assigned to Person. But the reverse is not allowed because Person lacks employeeId.
❓ Predict Output
intermediate2:00remaining
Assignment compatibility with union types
What will be the value of variable 'result' after this TypeScript code runs?
Typescript
type A = { x: number };
type B = { y: string };
let a: A = { x: 10 };
let b: B = { y: "hello" };
let result: A | B;
result = a;
console.log(result.x);Attempts:
2 left
💡 Hint
Check which properties are accessible on a union type when assigned a specific type.
✗ Incorrect
This code causes a TypeScript compilation error. Although 'result' is assigned an object of type A, its declared type is A | B, so accessing 'result.x' is invalid without type narrowing because 'x' does not exist on B.
🔧 Debug
advanced2:00remaining
Why does this assignment cause an error?
Why does the following TypeScript code cause a compilation error on the assignment line?
Typescript
interface Animal { name: string; }
interface Dog extends Animal { breed: string; }
let animal: Animal = { name: "Buddy" };
let dog: Dog = { name: "Max", breed: "Beagle" };
animal = dog; // OK
// dog = animal; // Error here
Attempts:
2 left
💡 Hint
Check which properties are required by the target type in the assignment.
✗ Incorrect
Assigning 'dog' to 'animal' works because 'dog' has all properties of 'animal'. But assigning 'animal' to 'dog' fails because 'animal' lacks the 'breed' property required by 'Dog'.
📝 Syntax
advanced2:00remaining
Which assignment is valid with type aliases?
Given these type aliases, which assignment is valid in TypeScript?
Typescript
type Point2D = { x: number; y: number };
type Point3D = { x: number; y: number; z: number };
let p2d: Point2D = { x: 1, y: 2 };
let p3d: Point3D = { x: 1, y: 2, z: 3 };
// Which assignment is valid?
Attempts:
2 left
💡 Hint
Think about whether an object with more properties can be assigned to a type requiring fewer properties.
✗ Incorrect
Assigning p3d to p2d is valid because p3d has all properties required by Point2D. The reverse is invalid because p2d lacks 'z'.
🚀 Application
expert2:00remaining
Determine the type compatibility in a function assignment
Consider these function types. Which assignment is allowed by TypeScript's assignment compatibility rules?
Typescript
type FuncA = (a: number, b: string) => void; type FuncB = (a: number) => void; let fA: FuncA = (a, b) => { console.log(a, b); }; let fB: FuncB = (a) => { console.log(a); }; // Which assignment is valid?
Attempts:
2 left
💡 Hint
A function accepting more parameters can be assigned to a type expecting fewer parameters (contravariance in parameters).
✗ Incorrect
In TypeScript, function parameter types are contravariant. fA (accepting number, string) can be assigned to FuncB (expecting number => void) because callers of fB provide only one argument, which fA can handle. The reverse (fB to FuncA) fails because fB lacks a handler for the second string parameter.