Skip to content

Commit 22d5a5c

Browse files
ktsnyyx990803
authored andcommitted
Migrate to TypeScript (#25)
* migrate to ts * more concrete type annoation * rewrite test code in TS * add circle ci config to use latest node version * update readme for more detailed requirement * update example code to ts
1 parent 63d5867 commit 22d5a5c

File tree

13 files changed

+132
-74
lines changed

13 files changed

+132
-74
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ node_modules
22
example/build.js
33
example/build.js.map
44
test/test.build.js
5+
6+
/index.js
7+
/index.d.ts

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
55
### Usage
66

7-
**Required**: Babel with stage 1 transforms, or TypeScript 1.5+ (for [decorators](https://github.com/wycats/javascript-decorators/blob/master/README.md)).
7+
**Required**: [ECMAScript stage 1 decorators](https://github.com/wycats/javascript-decorators/blob/master/README.md).
8+
If you use Babel, [babel-plugin-transform-decorators-legacy](https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy) is needed.
9+
If you use TypeScript, enable `--experimentalDecorators` flag.
810

911
Note:
1012

@@ -61,7 +63,7 @@ class App {
6163
### Build the Example
6264

6365
``` bash
64-
$ npm install && npm run build
66+
$ npm install && npm run example
6567
```
6668

6769
### License

circle.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
machine:
2+
node:
3+
version: 6

example/example.js renamed to example/example.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import Component from '../'
1+
import * as Vue from 'vue'
2+
import Component from '../index'
23

34
@Component({
45
props: {
56
propMessage: String
67
}
78
})
8-
class App {
9+
class App extends Vue {
10+
propMessage: string
11+
msg: number
12+
913
// return initial data
1014
data () {
1115
return {
@@ -28,13 +32,13 @@ class App {
2832
alert('greeting: ' + this.msg)
2933
}
3034

31-
render (h) {
35+
render (h: Vue.CreateElement) {
3236
return (
3337
h('div', [
3438
h('input', {
3539
domProps: { value: this.msg },
3640
on: {
37-
input: (event) => {
41+
input: (event: any) => {
3842
this.msg = event.target.value
3943
}
4044
}

example/tsconfig.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": [
5+
"dom",
6+
"es2015"
7+
],
8+
"module": "commonjs",
9+
"moduleResolution": "node",
10+
"isolatedModules": false,
11+
"experimentalDecorators": true,
12+
"noImplicitAny": true,
13+
"noImplicitThis": true,
14+
"strictNullChecks": true,
15+
"removeComments": true,
16+
"suppressImplicitAnyIndexErrors": true
17+
},
18+
"include": [
19+
"./**/*.ts"
20+
],
21+
"compileOnSave": false
22+
}

example/webpack.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
module.exports = {
2-
entry: './example/example.js',
2+
entry: './example/example.ts',
33
output: {
44
path: './example',
55
filename: 'build.js'
66
},
77
module: {
88
loaders: [
99
{
10-
test: /\.js$/,
10+
test: /\.ts$/,
1111
exclude: /node_modules|vue\/src/,
12-
loader: 'babel?stage=0'
12+
loader: 'ts'
1313
}
1414
]
1515
},

index.d.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

index.js renamed to index.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
Object.defineProperty(exports, '__esModule', {
2-
value: true
3-
})
1+
import * as Vue from 'vue'
42

5-
var Vue = require('vue')
6-
7-
var internalHooks = [
3+
const internalHooks = [
84
'data',
95
'beforeCreate',
106
'created',
@@ -19,13 +15,15 @@ var internalHooks = [
1915
'render'
2016
]
2117

22-
function componentFactory (Component, options) {
23-
if (!options) {
24-
options = {}
25-
}
26-
options.name = options.name || Component.name
18+
export type VueClass = { new (): Vue } & typeof Vue
19+
20+
function componentFactory (
21+
Component: VueClass,
22+
options: Vue.ComponentOptions<any> = {}
23+
): VueClass {
24+
options.name = options.name || (Component as any)._componentTag
2725
// prototype props.
28-
var proto = Component.prototype
26+
const proto = Component.prototype
2927
Object.getOwnPropertyNames(proto).forEach(function (key) {
3028
if (key === 'constructor') {
3129
return
@@ -35,7 +33,7 @@ function componentFactory (Component, options) {
3533
options[key] = proto[key]
3634
return
3735
}
38-
var descriptor = Object.getOwnPropertyDescriptor(proto, key)
36+
const descriptor = Object.getOwnPropertyDescriptor(proto, key)
3937
if (typeof descriptor.value === 'function') {
4038
// methods
4139
(options.methods || (options.methods = {}))[key] = descriptor.value
@@ -48,20 +46,21 @@ function componentFactory (Component, options) {
4846
}
4947
})
5048
// find super
51-
var superProto = Object.getPrototypeOf(Component.prototype)
52-
var Super = superProto instanceof Vue
53-
? superProto.constructor
49+
const superProto = Object.getPrototypeOf(Component.prototype)
50+
const Super: VueClass = superProto instanceof Vue
51+
? superProto.constructor as VueClass
5452
: Vue
5553
return Super.extend(options)
5654
}
5755

58-
function decorator (options) {
56+
export default function decorator (options: Vue.ComponentOptions<any>): <V extends VueClass>(target: V) => V
57+
export default function decorator <V extends VueClass>(target: V): V
58+
export default function decorator <V extends VueClass>(options: Vue.ComponentOptions<any> | V): any {
5959
if (typeof options === 'function') {
6060
return componentFactory(options)
6161
}
62-
return function (Component) {
62+
return function (Component: any) {
6363
return componentFactory(Component, options)
6464
}
6565
}
6666

67-
exports.default = decorator

package.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
"tsconfig.json"
1111
],
1212
"scripts": {
13-
"build": "webpack --config example/webpack.config.js",
13+
"build": "tsc -p .",
14+
"example": "npm run build && webpack --config example/webpack.config.js",
1415
"dev": "webpack --config example/webpack.config.js --watch",
15-
"test": "webpack --config test/webpack.config.js && mocha test/test.build.js"
16+
"test": "npm run build && webpack --config test/webpack.config.js && mocha test/test.build.js"
1617
},
1718
"repository": {
1819
"type": "git",
@@ -31,12 +32,14 @@
3132
},
3233
"homepage": "https://github.com/vuejs/vue-classy#readme",
3334
"devDependencies": {
34-
"babel-core": "^5.8.0",
35-
"babel-loader": "^5.3.3",
35+
"@types/chai": "^3.4.34",
36+
"@types/mocha": "^2.2.32",
3637
"chai": "^3.5.0",
37-
"mocha": "^2.4.5",
38+
"mocha": "^3.1.2",
3839
"node-libs-browser": "^1.0.0",
39-
"vue": "^2.0.0-rc.7",
40-
"webpack": "^1.12.12"
40+
"ts-loader": "^0.9.5",
41+
"typescript": "^2.0.6",
42+
"vue": "^2.0.3",
43+
"webpack": "^2.1.0-beta.25"
4144
}
4245
}

test/test.js renamed to test/test.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Component from '../index'
22
import { expect } from 'chai'
3-
import Vue from 'vue'
3+
import * as Vue from 'vue'
44

55
describe('vue-class-component', () => {
66

@@ -9,7 +9,7 @@ describe('vue-class-component', () => {
99
let destroyed = false
1010

1111
@Component
12-
class MyComp {
12+
class MyComp extends Vue {
1313
created () {
1414
created = true
1515
}
@@ -26,10 +26,10 @@ describe('vue-class-component', () => {
2626
})
2727

2828
it('methods', () => {
29-
let msg
29+
let msg: string = ''
3030

3131
@Component
32-
class MyComp {
32+
class MyComp extends Vue {
3333
hello () {
3434
msg = 'hi'
3535
}
@@ -42,7 +42,8 @@ describe('vue-class-component', () => {
4242

4343
it('computed', () => {
4444
@Component
45-
class MyComp {
45+
class MyComp extends Vue {
46+
a: number
4647
data () {
4748
return {
4849
a: 1
@@ -61,14 +62,15 @@ describe('vue-class-component', () => {
6162
})
6263

6364
it('other options', (done) => {
64-
let v
65+
let v: number
6566

6667
@Component({
6768
watch: {
6869
a: val => v = val
6970
}
7071
})
71-
class MyComp {
72+
class MyComp extends Vue {
73+
a: number
7274
data () {
7375
return { a: 1 }
7476
}
@@ -84,15 +86,17 @@ describe('vue-class-component', () => {
8486

8587
it('extending', function () {
8688
@Component
87-
class Base {
88-
data () {
89+
class Base extends Vue {
90+
a: number
91+
data (): any {
8992
return { a: 1 }
9093
}
9194
}
9295

9396
@Component
9497
class A extends Base {
95-
data () {
98+
b: number
99+
data (): any {
96100
return { b: 2 }
97101
}
98102
}

0 commit comments

Comments
 (0)