Skip to content

More concise way to specify an optional return type #41902

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
angryziber opened this issue Dec 9, 2020 · 4 comments
Closed
5 tasks done

More concise way to specify an optional return type #41902

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

Comments

@angryziber
Copy link

Suggestion

TypeScript already supports optional parameters and fields with a question mark.
Why not extend this feature to function/getter return types.

Use Cases/Examples

function optional(): string? {
  return undefined // or allow just plain return
}

this would allow defining of optional getters as well:

class Hello {
  optional1?: string
  get optional2(): string? {
    return undefined
  }
}

Alternative syntax would be to specify question mark after the name, or even after the braces:

function optional?(): string
function optional()?: string

However, I find myself trying the first version of the syntax every time, which currently results in compile error.

This would compliment the usage of question mark ? as nullablity/unefineability operator, similar to Kotlin.

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.
@MartinJohns
Copy link
Contributor

Duplicate of #35577.

@angryziber
Copy link
Author

The duplicate was closed, pointing to a discussion from 2016 that discusses everything about nulls. I was not able to follow/find the relevant parts in that discussion of why not to add nullable/undefineable return types.

By the end of 2020 more features have arrived to both JS and TS related to nullability handling, like optional chaining. It's time to reconsider and add the missing bits of nullability syntax to be able to make code more concise.

You can already get a nullable return type implicitly by e.g. returning the value from a nullable property (declared with ?), so why not allow to do that explicitly as well?

@MartinJohns
Copy link
Contributor

The point about optionality still stands. Optionality includes more than just "it can be undefined".

@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript labels Dec 10, 2020
@RyanCavanaugh
Copy link
Member

Echoing my comments in the other suggestion; we don't intend to do this. The use of T? as a shortcut was debated here

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

3 participants