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

Add Type declaration file #57

Merged
merged 5 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
24 changes: 15 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"dev": "rollup -c rollup.config.js -w",
"build": "rollup -c rollup.config.js",
"lint": "eslint src test example --fix",
"test": "jest",
"test": "npm run test:unit && npm run test:types",
"test:unit": "jest",
"test:types": "tsc -p types/test",
"dev:test": "jest --watch",
"prebuild": "npm run lint",
"pretest": "npm run build",
Expand All @@ -41,6 +43,7 @@
"rollup-plugin-buble": "^0.15.0",
"rollup-watch": "^3.2.2",
"rxjs": "^5.2.0",
"typescript": "^2.5.2",
"vue": "^2.2.4"
},
"eslintConfig": {
Expand Down
28 changes: 28 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Vue = require('vue')
import { WatchOptions } from 'vue'
import { Observable } from 'rxjs/Observable'

export type Observables = Record<string, Observable<any>>
declare module 'vue/types/options' {
interface ComponentOptions<V extends Vue> {
subscriptions?: Observables | ((this: V) => Observables)
domStreams?: string[]
Copy link
Member

Choose a reason for hiding this comment

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

We also have observableMethods in component options 😉

https://github.com/vuejs/vue-rx#createobservablemethodmethodname

}
}

declare module "vue/types/vue" {
interface Vue {
$observables: Observables;
$watchAsObservable(exprOrFn: string | Function, options?: WatchOptions): Observable<any>
Copy link
Member

Choose a reason for hiding this comment

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

I think we can improve this type by using type parameter like $watch.

$watchAsObservable<T>(fn: (this: this) => T, options?: WatchOptions): Observable<T>

Copy link
Member

Choose a reason for hiding this comment

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

Maybe, returned Observable has a value that is formed of { newValue: T, oldValue: T }?

https://github.com/vuejs/vue-rx#watchasobservableexporfn-options

$eventToObservable(event: string): Observable<any>
Copy link
Member

Choose a reason for hiding this comment

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

Looks like the returned type is Observable<{ name: string, msg: any }>

$subscribeTo<T>(
observable: Observable<T>,
next: (t: T) => void,
error: (e: any) => void,
complete: () => void): void
Copy link
Member

Choose a reason for hiding this comment

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

$fromDOMEvent(selector: string, event: string): Observable<any>
Copy link
Member

Choose a reason for hiding this comment

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

I guess we can let the return type be Observable<Event> since this function listens DOM event.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, https://github.com/vuejs/vue-rx/blob/master/src/methods/fromDOMEvent.js#L17 selector only applies to native dom element.

$createObservableMethod(methodName: string): Observable<any>
}
}

export declare function install(V: typeof Vue): void
73 changes: 73 additions & 0 deletions types/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import Vue = require('vue')
import * as VueRX from '../index'
import * as Rx from 'rxjs/Rx'

Vue.use(VueRX, Rx)

var vm = new Vue({
el: '#app',
subscriptions: {
msg: Rx.Observable.interval(100)
}
})

vm.$observables.msg.subscribe(msg => console.log(msg))

Vue.component('foo', {
subscriptions: function () {
return {
msg: Rx.Observable.interval(100)
}
}
})

new Vue({
domStreams: ['plus$']
})

var vm = new Vue({
data: {
a: 1
},
subscriptions () {
// declaratively map to another property with Rx operators
return {
aPlusOne: this.$watchAsObservable('a')
.pluck('newValue')
.map((a: number) => a + 1)
}
}
})

// or produce side effects...
vm.$watchAsObservable('a')
.subscribe(
({ newValue, oldValue }) => console.log('stream value', newValue, oldValue),
err => console.error(err),
() => console.log('complete')
)


var vm = new Vue({
created () {
this.$eventToObservable('customEvent')
.subscribe((event) => console.log(event.name,event.msg))
}
})

var vm = new Vue({
subscriptions () {
return {
inputValue: this.$fromDOMEvent('input', 'keyup').pluck('target', 'value')
}
}
})

var vm = new Vue({
subscriptions () {
return {
// requires `share` operator
formData: this.$createObservableMethod('submitHandler')
}
}
})
14 changes: 14 additions & 0 deletions types/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"lib": [
"es5",
"dom",
"es2015.promise",
"es2015.core"
],
"strict": true,
"noEmit": true
}
}
16 changes: 16 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"lib": [
"es5",
"dom",
"es2015.promise"
],
"strict": true,
"noEmit": true
},
"include": [
"*.d.ts"
]
}
4 changes: 4 additions & 0 deletions types/typings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "vue-rx",
"main": "index.d.ts"
}