You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This PR makes it possible to declare optional properties and methods in classes, similar to what is already permitted in interfaces. For example:
classBar{a: number;b?: number;f(){return1;}g?(): number;// Body of optional method can be omittedh?(){return2;}}
When compiled in --strictNullChecks mode, optional properties and methods automatically have undefined included in their type. Thus, the b property above is of type number | undefined and the g method above is of type (() => number) | undefined. Type guards can be used to strip away the undefined part of the type:
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 abstract modifier, to require that an implementation is provided by a subclass. If it's optional you don't have to override it meaning it should not be abstract.
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.
@Venryx I made an issue for this (#14417) but it was shot down. I don't think the guy reviewing the issue fully understood what I was getting at. You could create another issue and give a better explanation of the problem though. If you do let me know!
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
None yet
8 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: