Beginner
Use unknown, Not any
safeLength should accept a value of any kind and return its length if it is text, or null otherwise. Type the parameter as unknown (not any), give the function a return type, and narrow before using .length.
The checks also read the source: the word any must not appear.
What Check tests
- Your code has no type errors
- The text "hello" has length 5
- A number gives the fallback -1
- The word
anyis not used - The result can be null, so it can't be stored as a plain number
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Runs in your browser. Your code is saved here.
Press Run to see the page.
Console
Nothing printed yet.