Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to assert that the variable value is of type string.
Typescript
let value: any = "hello"; let strValue = value as [1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong type in the assertion.
Forgetting to use the
as keyword.✗ Incorrect
The
as keyword is used for type assertions in TypeScript. Here, value as string tells the compiler to treat value as a string.2fill in blank
mediumComplete the code to assert that the variable input is of type number.
Typescript
let input: any = 42; let numInput = [1] as number;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Asserting the wrong variable.
Placing
as before the variable.✗ Incorrect
You assert the type of the variable you want to convert. Here,
input as number asserts that input is a number.3fill in blank
hardFix the error in the code by completing the type assertion correctly.
Typescript
let data: any = "123"; let length = (data as [1]).length;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Asserting
data as number and trying to access length.Not using type assertion at all.
✗ Incorrect
To access the
length property, data must be asserted as a string.4fill in blank
hardFill both blanks to assert the variable item as a string and then get its length.
Typescript
let item: any = "typescript"; let len = ([1] as [2]).length;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the variable and type in the assertion.
Using the wrong type in the assertion.
✗ Incorrect
First, you assert
item as a string using as string. Then you can access the length property.5fill in blank
hardFill all three blanks to create a type assertion that converts input to a string and then gets the uppercase version.
Typescript
let input: any = "hello"; let upper = ([1] as [2]).[3]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
toLowerCase instead of toUpperCase.Asserting the wrong variable or type.
✗ Incorrect
You assert
input as a string and then call the toUpperCase() method to get the uppercase string.