PropType doesn't take undefined for optional props into account
See original GitHub issueVersion
2.6.11
Steps to reproduce
Create component with optional prop being an object and try to access its property
import Vue, { PropType } from 'vue';
Vue.extend({
props: {
container: Object as PropType<{ n: number }>,
// or container: Number
},
mounted () {
// should fail because this.container could be undefined
console.log(this.container.n);
},
});
What is expected?
TS error: Object is possibly undefined
What is actually happening?
No error
Note
It works as expected with Composition API plugin
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top Results From Across the Web
TypeScript optional properties not accepting undefined value
Based on the linked answer, I think you have to specify undefined as a possible type for the prop, so type?: string |...
Read more >How to validate React props using PropTypes - LogRocket Blog
Learn how to validate props with React PropTypes, React's internal mechanism for adding type checking to component props.
Read more >How to Convert Object Props With Undefined Type to Optional ...
This type will map over the object props and check if their type contains undefined (by comparing the original prop type with the...
Read more >React Pro Tip #2 — How to Type `this.props` to Include ...
This trick is for those occasions when you use a class component, ... TypeScript to complain about optional props being possibly undefined ......
Read more >Typechecking With PropTypes - React
React.PropTypes has moved into a different package since React v15.5. Please use the prop-types library instead. We provide a codemod script to automate...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yeah, I guess I didn’t emphasised actual problem clear enough, sorry and thanks
@posva you mean vue behaviour is correct and VCA behaviour is not?