Complete the code to declare a variable with the unknown type.
let value: [1];any instead of unknown disables type safety.void with unknown.The unknown type is used when the type is not known yet, unlike any which disables type checking.
Complete the code to assign a value of type unknown to a variable.
let data: unknown = [1];unknown with any in assignment.You can assign any value to a variable of type unknown. Here, 42 is a valid number.
Fix the error in the code by adding a type check before assignment.
let value: string; let input: unknown = 'test'; if (typeof input === [1]) { value = input; }
unknown directly without type check.Before assigning input of type unknown to value of type string, we check if input is a string.
Fill both blanks to safely assign an unknown value to a number variable.
let num: number; let val: unknown = 10; if (typeof val === [1]) { num = val [2] 0; }
Check if val is a number, then assign it to num. The + 0 ensures num is a number type.
Fill all three blanks to convert an any type to a string safely.
let input: any = 123; let output: string; if (typeof input === [1]) { output = input [2] ''; } else { output = String([3]); }
Check if input is a string, then concatenate with empty string to keep it string. Otherwise, convert input to string using String(input).