Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upOptional class properties #8625
Conversation
@@ -16,8 +14,6 @@ tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWith | |||
|
|||
class C { | |||
x?: number; // error |
This comment has been minimized.
This comment has been minimized.
mhegazy
commented
May 16, 2016
can you add a declaration emit test. |
mhegazy
commented
May 16, 2016
can you also add a test with |
mhegazy
commented
May 16, 2016
also another one for extending classes, making optionals non-optional, and making non-optionals optional. |
CreepGin
commented
Jun 3, 2016
•
@ahejlsberg Nice job! Just curious, can decorators fetch information that can determine whether the target is declared optional? |
mhegazy
commented
Jun 3, 2016
no, see #8126 |
Roam-Cooper
commented
Oct 2, 2016
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 |
Roam-Cooper
commented
Oct 3, 2016
Ah, yes, wasn't thinking straight! My bad. |
bradenhs
commented
Feb 24, 2017
•
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. |
Venryx
commented
Mar 15, 2017
@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. |
ahejlsberg commentedMay 16, 2016
•
edited
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: