0
0
Typescriptprogramming~10 mins

Extract type 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 extract the type of elements from the array.

Typescript
type ElementType = Extract<[1], string[]>;
Drag options to blanks, or click blank then click option'
Astring[]
Bnumber[]
Cboolean[]
Dany[]
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a type that is not part of the union.
Confusing the order of types in Extract.
2fill in blank
medium

Complete the code to extract only the string literal types from the union.

Typescript
type OnlyStrings = Extract<'a' | 1 | true, [1]>;
Drag options to blanks, or click blank then click option'
Anumber
Bboolean
Cstring
Dstring | number
Attempts:
3 left
💡 Hint
Common Mistakes
Using a union type instead of a single type.
Using the wrong primitive type.
3fill in blank
hard

Fix the error in the code to correctly extract the function type from the union.

Typescript
type FuncType = Extract<() => void | string, [1]>;
Drag options to blanks, or click blank then click option'
Astring
BFunction
Cvoid
D() => void
Attempts:
3 left
💡 Hint
Common Mistakes
Using Function which is too broad.
Extracting the wrong type from the union.
4fill in blank
hard

Fill both blanks to extract only the number types from the union and assign it to the variable.

Typescript
type NumbersOnly = Extract<[1], [2]>;
Drag options to blanks, or click blank then click option'
A'a' | 1 | 2 | true
Bnumber
Cstring
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of the types in Extract.
Using the wrong type to extract.
5fill in blank
hard

Fill all three blanks to extract the types assignable to string or boolean from the union.

Typescript
type Extracted = Extract<[1], [2] | [3]>;
Drag options to blanks, or click blank then click option'
A'hello' | 42 | false | true
Bstring
Cboolean
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using number instead of boolean.
Not using a union type in the second argument.