question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot Optionalize Class Getters

See original GitHub issue

TypeScript Version: 2.2.1

Code

class Person {
  firstName: string;
  lastName: string;

  get fullName?() {
    return this.firstName + ' ' + this.lastName;
  }
}

Expected behavior:

Since fullName is optional the following should work:

const Me: Person = {
  firstName: 'Foo',
  lastName: 'Bar'
}

Actual behavior:

A syntax error occurs on this line get fullName?() {

I’m not sure if omitting the ability to optionalize getters was by design or not. If it was by design, I’d love to know the reasoning behind it.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:45
  • Comments:31 (6 by maintainers)

github_iconTop GitHub Comments

22reactions
Venryxcommented, Mar 16, 2017

@mhegazy Like bradenhs said, class methods can be made optional, as shown in this merged PR: https://github.com/Microsoft/TypeScript/pull/8625

class Bar {
    h?() {
        return 2;
    }
}

As can be seen, it’s an optional method that also has a body/implementation specified.

That’s the same thing we’d like for class getters/setters, like so:

class Bar {
    get h?() {
        return 2;
    }
}

However, this doesn’t compile currently, which seems inconsistent when class methods work with it fine.

14reactions
RyanCavanaughcommented, Mar 16, 2017

@mhegazy seems inconsistent to allow (bodied) optional methods but not getters. Thoughts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript optional property with a getter - Stack Overflow
In a class that implements an interface with an optional readonly property, the class may leave off the getter. See this answer get...
Read more >
Returning an optional in getters - Code is poetry
A practice I've used recently and found quite helpful is to wrap the return type of newly created getters inside an Optional.
Read more >
getter - JavaScript - MDN Web Docs - Mozilla
The get syntax binds an object property to a function that will be called when that property is looked up. It can also...
Read more >
Using Optional with Jackson - Baeldung
In this case, isPresent() is a public getter on the Optional class. This means it will be serialized with a value of true...
Read more >
Swift properties / getters setters | Apple Developer Forums
public class Person { private var _id: Int = 0 private var _lastName: String ... _id; } set { if newValue < 0...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found