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 Result({ result }: { result?: IResult }): ReactElement {
let total = 6;
let analyzed = 0;
if (result) {
total = result.total;
analyzed = result.analyzed;
}
...
}
Error
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
Solution
Use the non-null assertion operator !
function Result({ result }: { result?: IResult }): ReactElement {
let total = 10;
let analyzed = 0;
if (result) {
total = result.total!; // non-null assertion operator
analyzed = result.analyzed!; // non-null assertion operator
}
...
}