Rather niche typescript question…
I have a class with a method that returns string | null
and I’d like to refine the type of objects of that class in situations where we know the method will return a string.
Anyone know how to do this?
Rather niche typescript question…
I have a class with a method that returns string | null
and I’d like to refine the type of objects of that class in situations where we know the method will return a string.
Anyone know how to do this?
Could you just use the postfix non-null assertion operator !
?
foo.maybeNullFunction()
can be either null or string , so the compiler can’t conclude which type the returned value is in your example. foo.maybeNullFunction()!
asserts the returned value can’t be null, so the compiler concludes it’s a string.