-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Optional class properties #8625
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
@@ -16,8 +14,6 @@ tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWith | |||
|
|||
class C { | |||
x?: number; // error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comments are out of date now.
can you add a declaration emit test. |
can you also add a test with |
also another one for extending classes, making optionals non-optional, and making non-optionals optional. |
@ahejlsberg Nice job! Just curious, can decorators fetch information that can determine whether the target is declared optional? |
no, see #8126 |
Optional class properties don't seem to work for abstract properties in typescript 2.0.3, subclasses of the abstract class are forced to implement the property. |
@Roam-Cooper that's the point of the |
Ah, yes, wasn't thinking straight! My bad. 😃 |
Does this PR enable making class getters optional? I can't figure out how to mark getters as optional. I have no idea where the question mark would go. |
@bradenhs I have the same question. Would be nice for my situation, where I want to make a custom derived class (which has getters) compatible with the base class definition/type-shape. |
This PR makes it possible to declare optional properties and methods in classes, similar to what is already permitted in interfaces. For example:
When compiled in
--strictNullChecks
mode, optional properties and methods automatically haveundefined
included in their type. Thus, theb
property above is of typenumber | undefined
and theg
method above is of type(() => number) | undefined
. Type guards can be used to strip away theundefined
part of the type: