Skip to content

Getters/setters are required when their corresponding type permits undefined, cannot be marked optional #46448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SelyankinFyodor opened this issue Oct 20, 2021 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@SelyankinFyodor
Copy link

Bug Report

It requires filed which is a getter and setter for optional filed in return value

πŸ”Ž Search Terms

getters setters existential type

πŸ•— Version & Regression Information

typescript version: 4.3.5

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

class innerProps {
    optfield1?: number;
    optfield2?: string;
}

class outerProp {
    __innerProps__?: innerProps;
    get innerProps(): innerProps | undefined {
        return this.__innerProps__;
    }
    set innerProps(value: innerProps | undefined) {
        this.__innerProps__ = value;
    }
    anotherOptionalProp?: number;
}

function failedFunc(): outerProp {
    return {anotherOptionalProp: 1};
}

πŸ™ Actual behavior

error: a required field is missing, although it is optional

πŸ™‚ Expected behavior

the returned object does not conflict with the declared type

@jcalz
Copy link
Contributor

jcalz commented Oct 20, 2021

Aw, I got excited when I saw "existential type" but this issue has nothing to do with them.

although it is optional

The innerProps property is not optional. It must be present even if its value is undefined. There's currently no way to mark accessor properties as optional (see #14417) so there's no simple way to fix this code to make it behave the way you want. The code you wrote is working as intended, and if you're looking for a way to make innerProps optional then this is probably a duplicate of #14417.

πŸ˜” I want existential types so much

@DanielRosenwasser
Copy link
Member

It doesn't really make sense to distinguish a missing getter from | undefined because that get accessor will always be installed. You might be better off mirroring the type of your class and using optional properties.

@DanielRosenwasser DanielRosenwasser changed the title getters setters existential type Getters/setters are required when their corresponding type permits undefined, cannot be marked optional Oct 21, 2021
@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 21, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants