Why does typescript says?
let shouldbeNumber: number Type 'unknown' is not assignable to type 'number'.Vetur(2322)
turnIntoNumber
will always get strings of numbers such as 1,2,3,4,5,6... , so I know shouldbeNumber
will always be a number. How can I convince typescript or develop this piece of code? :-)
async function makeMagic() {
let shouldbeNumber: number = await turnIntoNumber("2");
}
function turnIntoNumber(get) {
return new Promise((resolve, reject) => {
let shouldbeNumber : number = Number(get);
resolve(shouldbeNumber);
});
}
Would you please help me to turn this javascript into a better typescript? Thanks in advance.
Annotate the functions return type
function turnIntoNumber(get) : Promise<number> {
return new Promise((resolve, reject) => {
let shouldbeNumber : number = Number(get);
resolve(shouldbeNumber);
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With