question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PropType doesn't take undefined for optional props into account

See original GitHub issue

Version

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

image

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
jacekkarczmarczykcommented, Jun 12, 2020

Yeah, I guess I didn’t emphasised actual problem clear enough, sorry and thanks

3reactions
jacekkarczmarczykcommented, Jun 12, 2020

@posva you mean vue behaviour is correct and VCA behaviour is not?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found