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

feat: Add Annotating Props to Typescript page #2068

Merged
merged 7 commits into from
Apr 20, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/v2/guide/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,43 @@ const Component = Vue.extend({
```

If you find type inference or member completion isn't working, annotating certain methods may help address these problems. Using the `--noImplicitAny` option will help find many of these unannotated methods.



## Annotating Props

```ts
import Vue, { PropType } from 'vue'

interface ComplexMessage {
title: string,
okMessage: string,
cancelMessage: string
}
const Component = Vue.extend({
props: {
name: String,
success: { type: String },
callback: {
type: Function as PropType<()=>void>
pikax marked this conversation as resolved.
Show resolved Hide resolved
},
message: {
type: Object as PropType<ComplexMessage>,
required: true,
validation(message: ComplexMessage){
pikax marked this conversation as resolved.
Show resolved Hide resolved
return !!message.title;
}
},
notificationMessage: {
type: Object as PropType<ComplexMessage>,
required: true,
// without argument type
validation: function(message){
return !!message.title;
} as (arg: ComplexMessage) => boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think anyone would ever do this, and it's very similar to the previous example. A better one would be notificationMessage: {...} as PropValidator<ComplexMessage> with no annotation on the validator or type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense

}
}
})
```
If you find type inference or member completion isn’t working, annotating prop with `PropValidator<T>` may help address it.