0
0
Typescriptprogramming~10 mins

Runtime type checking strategies 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 check if a variable is a string at runtime.

Typescript
function isString(value: unknown): boolean {
  return typeof value === [1];
}
Drag options to blanks, or click blank then click option'
A"string"
B"number"
C"boolean"
D"object"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'number' or 'boolean' instead of 'string' in the typeof check.
Forgetting to use quotes around the type string.
2fill in blank
medium

Complete the code to check if a value is an array at runtime.

Typescript
function isArray(value: unknown): boolean {
  return Array.[1](value);
}
Drag options to blanks, or click blank then click option'
AisObject
Bfrom
Cof
DisArray
Attempts:
3 left
💡 Hint
Common Mistakes
Using Array.of or Array.from which create arrays but don't check types.
Using a non-existent method like isObject.
3fill in blank
hard

Fix the error in the code to check if a value is a number at runtime.

Typescript
function isNumber(value: unknown): boolean {
  return typeof value === [1];
}
Drag options to blanks, or click blank then click option'
Anumber
B'number'
C"number"
DNumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using the bare word number without quotes causes a syntax error.
Using the Number object instead of the string type name.
4fill in blank
hard

Fill both blanks to create a runtime check for an object that is not null.

Typescript
function isObject(value: unknown): boolean {
  return typeof value === [1] && value !== [2];
}
Drag options to blanks, or click blank then click option'
A"object"
Bnull
C"null"
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing value !== "null" instead of null.
Not checking for null and getting false positives.
5fill in blank
hard

Fill all three blanks to create a runtime check for a string array.

Typescript
function isStringArray(value: unknown): boolean {
  return Array.[1](value) && value.every(item => typeof item === [2] && item !== [3]);
}
Drag options to blanks, or click blank then click option'
AisArray
B"string"
Cnull
D"object"
Attempts:
3 left
💡 Hint
Common Mistakes
Using typeof item === 'object' instead of 'string'.
Not checking for null values inside the array.