"Optional" return type for functions (and getters) #35577
Labels
Declined
The issue was declined as something which matches the TypeScript vision
Suggestion
An idea for TypeScript
Search Terms
nullable return type, optional return type
Suggestion
I would like to be able to indicate that a function or getter might return
undefined
instead of the return type, using?
instead of having to apply| undefined
at the end of the return type in the function signature.I believe it increases orthogonality of the language, as
?
is used to indicate that fields and input arguments to functions might beundefined
. It would therefore seem logical to be able to use this operator form function return types as well.Use Cases
It seems odd that I have to use
| undefined
for the return type of functions when I can use the?
operator for fields (and input arguments). To me, the semantic meaning of?
would be the same in both cases, signifying that the value might beundefined
, so I think that it could make the language a bit better.Examples
I would like to be able to write
instead of
Another example:
?
is used here as meaning| undefined
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: