TypeScript is JavaScript with syntax for types. TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.
How to reproduce it
function showUserData(userName?: string) {
const name: string = userName;
const age: number = 32;
console.log(`Name : ${name}`);
console.log(`Age: ${age}`);
}
showUserData();
Error
error TS2322: Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
Solution
Use an exclamation mark.
function showUserData(userName?: string) {
const name: string = userName!; // exclamation mark
const age: number = 32;
console.log(`Name : ${name}`);
console.log(`Age: ${age}`);
}
showUserData();