Type Assertions in TypeScript
Learn how to use type assertions in TypeScript to explicitly cast variables to specific types when their types are not inferred correctly, ensuring type safety in your code.
Learn how to use type assertions in TypeScript to explicitly cast variables to specific types when their types are not inferred correctly, ensuring type safety in your code.
let userInput: unknown = "123";
let parsedInput: number = (userInput as string).length;
console.log(parsedInput); // Outputs: 3