Skip to content
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

Optional properties that are not set on nested objects cause errors #5759

Open
massimonewsuk opened this issue Feb 2, 2018 · 8 comments
Open

Comments

@massimonewsuk
Copy link

interface Row {
    status: string | null;
    name: string;
}

interface Data {
    nested: {
        status?: string;
        name: string;
    };
}

function mapData(row: Row): Data {
    const entity: Data = {
        nested: {
            name: row.name,
        }
    };
    if (row.status !== null) {
        entity.nested.status = row.status;
    }
    return entity;
}

Reproducible on the flow try thingy... Just copy and paste the code above.

This works fine in TypeScript.

@apsavin
Copy link
Contributor

apsavin commented Feb 2, 2018

It seems that it's possible to avoid this error if you avoid interfaces: try

@ghost
Copy link

ghost commented Feb 2, 2018

Alternatively, you could create a type for nested.

Try Flow

@massimonewsuk
Copy link
Author

@idiostruct In this case the nested doesn't really make sense to have a type

@apsavin Thanks for letting me know I can use types, that workaround works for me. However, it still looks like this is a bug.

@massimonewsuk
Copy link
Author

massimonewsuk commented Feb 2, 2018

@TrySound thanks for the smaller piece of code... the original codebase I was having this in was several thousand lines and took me almost 20 minutes to get it down to my original example... I'm glad you could distil it further 😄

@ajs139
Copy link

ajs139 commented Mar 2, 2018

@massimonewsuk
Copy link
Author

Still an issue in v0.67.0

@massimonewsuk
Copy link
Author

Still an issue in v0.70.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants