TS18048
error TS18048: ‘
text
‘ is possibly ‘undefined
‘.
Broken Code ❌
1 |
|
Fixed Code ✔️
If the text
parameter is optional, it can potentially be of type unknown
. When the type is unknown
, accessing methods like Array.slice is not possible.
To solve this error, we need to employ a type guard to ensure that text
is of type string
:
1 |
|
Note: We also need to employ a default return value to maintain the function return type of string
.