Vue and TypeScript required prop

Viewed 4312

I added a required prop to my component class using vue-property-decorator, but when I tried using the component without the prop, I didn't see any console errors that indicate the required prop is missing. Why?

export default class Test extends Vue {
  @Prop() private message!: string;
}

The following code yields no errors as expected:

<test message="Hello" />

The following code should result in an error but doesn't:

<test />
1 Answers

The @Prop decorator takes a PropOptions object, which contains a required property with a default value of false. To make message required, specify required: true in your @Prop declaration:

@Prop({ required: true }) private message!: string;

Edit Vue Template

Related