FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Optional class properties by ahejlsberg · Pull Request #8625 · microsoft/TypeScript · GitHub

Optional class properties - #8625

Merged
Anders Hejlsberg (ahejlsberg) merged 11 commits into
masterfrom
optionalClassProperties
May 18, 2016
Merged

Optional class properties#8625
Anders Hejlsberg (ahejlsberg) merged 11 commits into
masterfrom
optionalClassProperties

Conversation

Anders Hejlsberg (ahejlsberg) commented May 16, 2016
edited
Loading

Copy link
Copy Markdown
Member

This PR makes it possible to declare optional properties and methods in classes, similar to what is already permitted in interfaces. For example:

class Bar {
    a: number;
    b?: number;
    f() {
        return 1;
    }
    g?(): number;  // Body of optional method can be omitted
    h?() {
        return 2;
    }
}

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:

function test(x: Bar) {
    x.a;  // number
    x.b;  // number | undefined
    x.f;  // () => number
    x.g;  // (() => number) | undefined
    let f1 = x.f();            // number
    let g1 = x.g && x.g();     // number | undefined
    let g2 = x.g ? x.g() : 0;  // number
}

@@ -16,8 +14,6 @@ tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWith

class C {
x?: number; // error

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

comments are out of date now.

Copy link
Copy Markdown
Contributor

can you add a declaration emit test.

Copy link
Copy Markdown
Contributor

can you also add a test with --strictNullchecks and an optional parameter property declaration, e.g. constructor(private a?: number) { }

Copy link
Copy Markdown
Contributor

also another one for extending classes, making optionals non-optional, and making non-optionals optional.

David (CreepGin) commented Jun 3, 2016
edited
Loading

Copy link
Copy Markdown

Anders Hejlsberg (@ahejlsberg) Nice job! Just curious, can decorators fetch information that can determine whether the target is declared optional?

Copy link
Copy Markdown
Contributor

can decorators fetch information that can determine whether the target is declared optional?

no, see #8126

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Contributor

Fennec Cooper (@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.

Copy link
Copy Markdown

Ah, yes, wasn't thinking straight! My bad. 😃

Braden Snell (bradenhs) commented Feb 24, 2017
edited
Loading

Copy link
Copy Markdown

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.

Copy link
Copy Markdown

Braden Snell (@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.

Copy link
Copy Markdown

Stephen Wicklund (@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!

Microsoft (microsoft) locked and limited conversation to collaborators Jun 19, 2018
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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants


Back | FazBrowse Home | New Git URL