When I have a function like this, I get no complaints:
const roundToTwo = (num: number) => {
return (Math.round( (num "e 2")) "e-2");
};
When I hover over the function name in VS Code, I can see that it's returning a number: const roundToTwo: (num: number) => number
.
But when I try to define the return type like this:
const roundToTwo: number = (num: number) => {
return (Math.round( (num "e 2")) "e-2");
};
I get this error:
Type '(num: number) => number' is not assignable to type 'number'.
Why is this or what am I doing wrong?
CodePudding user response:
try like this shape
type roundToTwo=(num:number)=>number
const roundToTwo :roundToTwo= (num:number)=> {
return (Math.round( (num "e 2")) "e-2");
};
CodePudding user response:
The error shown is self explanatory. The return type of roundToTwo
is (num: number) => number
and not just number
. When you are already specifying a return type through the function indirectly (Typescript does it though), specifying a primitive directly is not right.
The right way:
const roundToTwo: (num: number) => number = function (num: number) {
return (Math.round( (num "e 2")) "e-2");
};
Or simply leave it without specifying the type directly. Typescript anyways applies the type since the initialization takes place right at the time of declaration.
CodePudding user response:
Since you're confused by both the syntax and semantics, I'm providing a detailed explanation.
roundToTwo
is a function type, not a number
Given this defintion:
const roundToTwo = (num: number) => {
return (Math.round( (num "e 2")) "e-2")
}
roundToTwo
is a function not a number. It has a function type. In the above declaration, it is inferred to have the function type (num: number) => number
because that is the type of the function expression being assigned to it.
Remember, in Javascript, functions are first class objects. Thus in Typescript, they have their own type.
But const roundToTwo: number
defines it as a number
That is what you mistakenly did at the start of your new definition. You said, "roundToTwo is a number".
const roundToTwo: number = (num: number) => {
return (Math.round( (num "e 2")) "e-2")
}
// compare the above to:
const roundToTwo: number = 2
const n: number = 2
There's no need to explicitly type it
It's completely unnecessary to explicitly type roundToTwo
since you assign its value immediately, and the iferred type is what you want anyway. Just like you don't need to add : number
to this declaration:
const max = 42 // same as "const max: number = 42"
But if you need to, you have three options.
The ugly verbose one:
const roundToTwo: (num: number) => number = (num: number) => {
return (Math.round( (num "e 2")) "e-2")
}
The compact one:
const roundToTwo = (num: number):number => {
return (Math.round( (num "e 2")) "e-2")
}
The readable one:
type numericFunction = (num: number) => number
const roundToTwo: numericFunction = (num: number) => {
return (Math.round( (num "e 2")) "e-2")
}
The readable one is also very useful if you have to reference this type elsewhere, e.g. as a function parameter:
function scaleArray(arr: number[], scaleFunc: numericFunction): number[ {
}