Skip to content

"Optional" return type for functions (and getters) #35577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
rosenbjerg opened this issue Dec 9, 2019 · 4 comments
Closed
5 tasks done

"Optional" return type for functions (and getters) #35577

rosenbjerg opened this issue Dec 9, 2019 · 4 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@rosenbjerg
Copy link

rosenbjerg commented Dec 9, 2019

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 be undefined. 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 be undefined, so I think that it could make the language a bit better.

Examples

I would like to be able to write

function doStuff(myParam:number) : number? {
    if (myParam !== 17) return 42 + myParam;
}

instead of

function doStuff(myParam:number) : number | undefined {
    if (myParam !== 17) return 42 + myParam;
}

Another example:

interface MyInterface {
    tryFindNumber: () => number?
}

? is used here as meaning | undefined

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript labels Dec 9, 2019
@RyanCavanaugh
Copy link
Member

See #7426 where this was discussed at length and decided against

@rosenbjerg
Copy link
Author

That discussion seems to be about
public doStuff(myParam?:number) : void
vs
public doStuff(myParam:number?) : void
but this one only concerns the return type of functions and getters.
I have updated my examples to make that clearer

@rosenbjerg rosenbjerg changed the title Nullable return types for functions (and getters) "Optional" return type for functions (and getters) Dec 9, 2019
@RyanCavanaugh
Copy link
Member

We don't have special syntax for type annotations that only works in certain positions and would not add such a thing.

@rosenbjerg
Copy link
Author

rosenbjerg commented Dec 11, 2019

That is very understandable.

I can see it would need to be:

function doStuff?(myParam:number) : number {
    if (myParam !== 17) return 42 + myParam;
}

If it should follow the same pattern as properties, and that my previous example would have required what was decided against to make sense.

Thank you for your answers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants