Struggling with types in TypeScript while trying to parse a cookie item using js-cookie:
// the item 'number' contains a javascript number (ex:5)
let n:number
if(typeof Cookies.get('number')!== 'undefined'){
n = JSON.parse(Cookies.get('number'))
}else{
n = 0
}
Typescript: Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'
Referring to the example in TypeScript's documentation: https://www.typescriptlang.org/docs/handbook/2/narrowing.html
Is there an error in this code snippet?