Skip to content

Commit

Permalink
feat: add types (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
gd4Ark authored Nov 11, 2020
1 parent 78c346e commit c92d7ac
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## 在 TypeScript 中指定组件的类型

```html
<template>
<upload-to-ali ref="uploadToAli" v-model="url" />
</template>
<script lang="ts">
// 需要引入这个
// import { UploadToAliType } from '@femessage/upload-to-ali'
export default {
data() {
return {
url: ''
}
},
mounted() {
(this.$refs.uploadToAli as UploadToAliType).dir = 'images/'
},
}
</script>
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"browser": {
"./sfc": "src/upload-to-ali.vue"
},
"types": "src/upload-to-ali.d.ts",
"scripts": {
"dev": "vue-styleguidist server",
"test": "jest",
Expand Down
90 changes: 90 additions & 0 deletions src/upload-to-ali.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import Vue, {VueConstructor} from 'vue'

declare module '@femessage/upload-to-ali' {
class FemessageComponent extends Vue {
static install(vue: typeof Vue): void
}

type CombinedVueInstance<
Instance extends Vue,
Data,
Methods,
Computed,
Props
> = Data & Methods & Computed & Props & Instance

type ExtendedVue<
Instance extends Vue,
Data,
Methods,
Computed,
Props
> = VueConstructor<
CombinedVueInstance<Instance, Data, Methods, Computed, Props> & Vue
>

type Combined<Data, Methods, Computed, Props> = Data &
Methods &
Computed &
Props

type UploadToAliData = {
previewUrl: string

uploading: boolean

isHighlight: boolean
}

type UploadToAliMethods = {
selectFiles: () => void
}

type UploadToAliComputed = {
uploadList: any[]
canUpload: boolean
uploadRequest: (file: any) => Promise<string>
}

type UploadToAliProps = {
action: string
bucket: string
region: string
dir: string
customDomain: string
value: string | any[]
multiple: boolean
size: number
accept: string
timeout: number
disabled: boolean
max: number
compressOptions: {[key: string]: any}
uploadOptions: {[key: string]: any}
preview: boolean
tip: string
onClick: (url: string, isFile: boolean) => void
beforeUpload: (files: any[]) => Promise<any>
onOversize: (fileOvesize: any) => void
request: (file: any) => Promise<string>
}

type UploadToAli = Combined<
UploadToAliData,
UploadToAliMethods,
UploadToAliComputed,
UploadToAliProps
>

export interface UploadToAliType extends FemessageComponent, UploadToAli {}

const UploadToAliConstruction: ExtendedVue<
Vue,
UploadToAliData,
UploadToAliMethods,
UploadToAliComputed,
UploadToAliProps
>

export default UploadToAliConstruction
}

0 comments on commit c92d7ac

Please sign in to comment.