Svelte: "x was created without expected prop y" false alert with binding when prop = undefined

Created on 24 Feb 2020  路  6Comments  路  Source: sveltejs/svelte

https://svelte.dev/repl/65985a2e42fe4b899a379750fe609447?version=3.19.1

<script>
    import Component from './Component.svelte'
    let value = undefined
</script>

<Component bind:value />

<Component> was created without expected prop 'value'

a fix for this would involve replacing a ton of test results so I'd suggest addressing #4454 at the same time

bug

Most helpful comment

I think this is not a bug, as let value = undefined; is the same as let value;.
Maybe we should support optional props.

All 6 comments

I think this is not a bug, as let value = undefined; is the same as let value;.
Maybe we should support optional props.

Same here.

This is very annoying to see warnings like these (and these).

I had this code

  export let selection;

And got these warnings

 <Component> was created without expected prop 'selection'

Changing it to

 export let selection = "";

silenced the warnings.

@paulschreiber it's not ideal because now your code is suggesting that selection always expects a string, which would be incorrect self-documenting code in all but a few cases.

@oatymart what's the proper way to solve this?

Use the appropriate default according to type for each of your props that deserves one, or continue to omit it if that makes more sense. Just don't expect to achieve perfection because it's all a tradeoff between warnings, readability, and convenience.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thoughtspile picture thoughtspile  路  3Comments

plumpNation picture plumpNation  路  3Comments

bestguy picture bestguy  路  3Comments

davidcallanan picture davidcallanan  路  3Comments

lnryan picture lnryan  路  3Comments