Reputation: 15414
I am new to React/Typescript. I have a function that takes a number and outputs a string, as such:
const getCurrentLevel = (wordCount: number): Level => {
if (wordCount <= 3) {
return 'Low';
}
if (wordCount <= 6) {
return 'Med';
}
return 'High';
};
I then call this function below:
let helperLevel = getCurrentLevel(numWords);
And pass that to another component:
<helperText level={helperLevel} />
All this works fine, but I get a warning on compilation.
Cannot find name 'Level'.
On the line:
const getCurrentLevel = (wordCount: number): Level => {
I'm not sure what this means or now to fix it. As I understand, I must 'cast' this as "Level" as above. But I am not sure how to go about fixing this or what the error means.
Can anyone point me in the right direction?
Upvotes: 0
Views: 47
Reputation: 10264
type Level = 'Low' | 'Med' | 'High'
declare this line.
Added
(wordCount: number): Level => { }
means it has one parameter as number
type, it returns Level
type.
In your case, you return 'High', 'Low' and 'Med' you can declare like above that.
Upvotes: 2