0
0
Typescriptprogramming~20 mins

How assignment compatibility is checked in Typescript - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TypeScript Assignment Compatibility Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
AAlice
Bundefined
CTypeScript compilation error
DBob
Attempts:
2 left
💡 Hint
Think about whether a type with more properties can be assigned to a type with fewer properties.
Predict Output
intermediate
2: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);
A10
Bhello
CTypeScript compilation error
Dundefined
Attempts:
2 left
💡 Hint
Check which properties are accessible on a union type when assigned a specific type.
🔧 Debug
advanced
2: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
ABecause 'animal' lacks the 'breed' property required by 'Dog'
BBecause 'dog' lacks the 'name' property required by 'Animal'
CBecause TypeScript does not allow assigning interfaces
DBecause 'animal' and 'dog' are unrelated types
Attempts:
2 left
💡 Hint
Check which properties are required by the target type in the assignment.
📝 Syntax
advanced
2: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?
Ap3d = p2d;
Bp2d = p3d;
Cp2d = { x: 1 };
Dp3d = { x: 1, y: 2 };
Attempts:
2 left
💡 Hint
Think about whether an object with more properties can be assigned to a type requiring fewer properties.
🚀 Application
expert
2: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?
AfB = fA;
BBoth assignments are valid
CNeither assignment is valid
DfA = fB;
Attempts:
2 left
💡 Hint
A function accepting more parameters can be assigned to a type expecting fewer parameters (contravariance in parameters).