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

A bug when a field has undefined value #39

Closed
plantain-00 opened this issue Nov 29, 2016 · 7 comments
Closed

A bug when a field has undefined value #39

plantain-00 opened this issue Nov 29, 2016 · 7 comments

Comments

@plantain-00
Copy link

just use vue, this works

data: {
    key: undefined
}

use vue-class-component, the key is not found

class Foo extends Vue {
    key = undefined;
}

I checked the source code, should be: https://github.com/vuejs/vue-class-component/blob/master/src/data.ts#L23

if (data[key] !== undefined) {
  plainData[key] = data[key]
}

the type of my key is undefined | null, so I cannot use null to replace undefined.

@ktsn
Copy link
Member

ktsn commented Nov 29, 2016

This restriction is needed because it will accidentally collect undefined value if we use babel + some property decorators. I'll write about this into readme later.
Work around is just use data() hook.

@ktsn ktsn closed this as completed Nov 29, 2016
@plantain-00
Copy link
Author

@ktsn
For case like:

class Foo extends Vue {
    key1 = 1;
    key2 = undefined;
}

use data() hook

means

class Foo extends Vue {
    key1 = 1;
    data () {
        return {
            key2: undefined
        };
    }
}

or

class Foo extends Vue {
    data () {
        return {
            key1: 1,
            key2: undefined
        };
    }
}

or something else? Sorry I didn't follow.

@ktsn
Copy link
Member

ktsn commented Nov 29, 2016

@plantain-00 Yes, you are right.

@shulhi
Copy link

shulhi commented Apr 12, 2017

@ktsn Is this ever get fixed?

@johnhargrove
Copy link

This behavior cost me quite a lot of time. Is there documentation why it is the way it is @ktsn ? I checked the README but it didn't have the aforementioned explanation. So, you can do:

@Component({})
export default class RegisterComponent extends Vue {
  private firstName: string;
}

And end up with strange errors about the field being missing even though it's CLEARLY there. Very counter intuitive.

I think this is made worse by the TypeScript strictNullChecks compiler flag not catching undefined fields. That battle seems to be waging on in microsoft/TypeScript#8476

Thank you for your help!

@ktsn
Copy link
Member

ktsn commented May 5, 2017

Sorry, I totally forgot to write about this behavior in readme. I've just done. cb8d9de

@johnhargrove
Copy link

Thank you for that

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

No branches or pull requests

4 participants