0
0
Typescriptprogramming~10 mins

Intersection type syntax in Typescript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a variable with an intersection type of Person and Employee.

Typescript
type Person = { name: string };
type Employee = { id: number };
let worker: Person [1] Employee;
Drag options to blanks, or click blank then click option'
A&
B|
C,
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '|' instead of '&' which creates a union type.
Using ',' or '+' which are invalid in type declarations.
2fill in blank
medium

Complete the code to define a function parameter with an intersection type of A and B.

Typescript
type A = { a: number };
type B = { b: string };
function combine(param: A [1] B) {
  console.log(param.a, param.b);
}
Drag options to blanks, or click blank then click option'
A|
B&
C/
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '|' which allows either type but not both.
Using invalid symbols like '/' or '+'.
3fill in blank
hard

Fix the error in the intersection type declaration.

Typescript
type X = { x: number } [1] { y: string };
Drag options to blanks, or click blank then click option'
A|
B+
C,
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using '|' which creates a union type.
Using ',' or '+' which are invalid here.
4fill in blank
hard

Fill both blanks to create an intersection type combining interfaces A and B.

Typescript
interface A { a: number; }
interface B { b: string; }
type AB = [1] A [2] B;
Drag options to blanks, or click blank then click option'
Atype
B&
C|
Dinterface
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'interface' instead of 'type' for intersection types.
Using '|' instead of '&' to combine types.
5fill in blank
hard

Fill all three blanks to create a variable with an intersection type and assign a valid object.

Typescript
type C = { c: boolean };
type D = { d: number };
let obj: [1] = { c: true, d: 42 } as [2] [3] D;
Drag options to blanks, or click blank then click option'
AC & D
BC
C&
DD
Attempts:
3 left
💡 Hint
Common Mistakes
Using only one type without intersection.
Using '|' instead of '&' for intersection.