Skip to content

Commit 5e2becf

Browse files
committed
#174: Migrate the project to Typescript. Therefore use vue cli migration tooling and integrate eslint again. Trying to upgrade the vue script code carefully to TypeScript without loosing to much of the old examples logic.
1 parent 3a9c2eb commit 5e2becf

26 files changed

+4078
-1576
lines changed

README.md

+112-3
Original file line numberDiff line numberDiff line change
@@ -1179,9 +1179,6 @@ vue create hello-vue3
11791179
11801180
I aligned my project to match the latest skeleton generation much better: So router, store and api got their own directories. The views are now in the correct folder `views` - and I extracted one component to use from the newly introduced `Home.vue` view: the `HelloSpringWorld.vue` component.
11811181
1182-
1183-
1184-
11851182
I also went over the [package.json](frontend/package.json) and upgraded to the latest release versions instead of alphas (except `@vue/test-utils` which only has a `rc` atm).
11861183
11871184
All imports were refactored too. Coming from this style:
@@ -1255,7 +1252,119 @@ Luckily this so answer helped me out: https://stackoverflow.com/a/65111966/49645
12551252
And finally Bootstrap Vue doesn't support Vue 3.x right now: https://github.com/bootstrap-vue/bootstrap-vue/issues/5196 - So I temporarily commented out the imports.
12561253
12571254
1255+
#### Add TypeScript
1256+
1257+
Vue 3.x is now build with TypeScript: https://v3.vuejs.org/guide/typescript-support.html
1258+
1259+
> A static type system can help prevent many potential runtime errors as applications grow, which is why Vue 3 is written in TypeScript. This means you don't need any additional tooling to use TypeScript with Vue - it has first-class citizen support.
1260+
1261+
There's also a huge documentation of TypeScript itself at https://www.typescriptlang.org/docs/ I can also recommend https://medium.com/js-dojo/adding-typescript-to-your-existing-vuejs-2-6-app-aaa896c2d40a
1262+
1263+
To migrate your project there's the command:
1264+
1265+
```shell
1266+
vue add typescript
1267+
```
1268+
1269+
The first question arises: `Use class-style component syntax? (Y/n)` whether to use class-style component syntax or not. I didn't use it. I think the interface definitions of components are concise enough without the class-style. But let's see how this will work out.
1270+
1271+
So this was the output:
1272+
1273+
```shell
1274+
vue add typescript
1275+
WARN There are uncommitted changes in the current repository, it's recommended to commit or stash them first.
1276+
? Still proceed? Yes
1277+
1278+
📦 Installing @vue/cli-plugin-typescript...
1279+
1280+
1281+
added 59 packages, removed 58 packages, and audited 2219 packages in 6s
1282+
1283+
85 packages are looking for funding
1284+
run `npm fund` for details
1285+
1286+
3 low severity vulnerabilities
1287+
1288+
To address all issues (including breaking changes), run:
1289+
npm audit fix --force
1290+
1291+
Run `npm audit` for details.
1292+
✔ Successfully installed plugin: @vue/cli-plugin-typescript
1293+
1294+
? Use class-style component syntax? No
1295+
? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? Yes
1296+
? Use TSLint? Yes
1297+
? Pick lint features: Lint on save
1298+
? Convert all .js files to .ts? Yes
1299+
? Allow .js files to be compiled? Yes
1300+
? Skip type checking of all declaration files (recommended for apps)? Yes
1301+
1302+
🚀 Invoking generator for @vue/cli-plugin-typescript...
1303+
📦 Installing additional dependencies...
1304+
1305+
1306+
added 2 packages, and audited 2221 packages in 3s
1307+
...
1308+
✔ Successfully invoked generator for plugin: @vue/cli-plugin-typescript
1309+
```
1310+
1311+
Now I went through all the componentes and views and extended `<script>` to `<script lang="ts">`.
1312+
1313+
Also I changed
1314+
1315+
```javascript
1316+
export default {
1317+
```
1318+
1319+
to
1320+
1321+
```javascript
1322+
import { defineComponent } from 'vue';
1323+
1324+
export default defineComponent({
1325+
```
1326+
1327+
Now we need to transform our JavaScript code into TypeScript.
1328+
1329+
A really good introduction could be found here: https://www.vuemastery.com/blog/getting-started-with-typescript-and-vuejs/
1330+
1331+
> This process will take a while, depending on your code - and mainly on your knowledge about TypeScript. But I think it's a great path to go!
1332+
1333+
Don't forget to deactivate source control for `.js` and `.map` files in `src`, because these will now be generated (aka transpiled) from TypeScript and [shouldn't be checked in (anymore)](https://stackoverflow.com/a/26464907/4964553).
1334+
1335+
I enhanced my [frontend/.gitignore](frontend/.gitignore) like this:
12581336

1337+
```shell
1338+
# TypeScript
1339+
*.map
1340+
src/*.js
1341+
test/*.js
1342+
```
1343+
1344+
##### Vuex Store with TypeScript
1345+
1346+
According to https://next.vuex.vuejs.org/guide/typescript-support.html#typing-store-property-in-vue-component in order to use vuex store with TypeScript, we:
1347+
1348+
> must declare your own module augmentation.
1349+
1350+
TLDR; we need to create a file [src/vuex.d.ts](frontend/src/vuex.d.ts):
1351+
1352+
```javascript
1353+
import { ComponentCustomProperties } from 'vue'
1354+
import { Store } from 'vuex'
1355+
1356+
declare module '@vue/runtime-core' {
1357+
// declare your own store states
1358+
interface State {
1359+
count: number
1360+
}
1361+
1362+
// provide typings for `this.$store`
1363+
interface ComponentCustomProperties {
1364+
$store: Store<State>
1365+
}
1366+
}
1367+
```
12591368

12601369

12611370

frontend/.eslintignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tests/unit/coverage
2+
tests/e2e/reports
3+
tests/e2e/custom-commands
4+
src/views/Bootstrap.vue

frontend/.eslintrc.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
node: true
5+
},
6+
'extends': [
7+
'plugin:vue/vue3-essential',
8+
'eslint:recommended',
9+
'@vue/typescript/recommended'
10+
],
11+
parserOptions: {
12+
ecmaVersion: 2020
13+
},
14+
rules: {
15+
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
16+
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
17+
},
18+
overrides: [
19+
{
20+
files: [
21+
'**/__tests__/*.{j,t}s?(x)',
22+
'**/tests/unit/**/*.spec.{j,t}s?(x)'
23+
],
24+
env: {
25+
jest: true
26+
}
27+
}
28+
]
29+
}

frontend/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# TypeScript
2+
*.map
3+
src/*.js
4+
test/*.js
5+
16
.DS_Store
27
node_modules/
38
/dist/

frontend/README.md

-28
This file was deleted.

frontend/jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
preset: '@vue/cli-plugin-unit-jest',
2+
preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel',
33
transform: {
44
'^.+\\.vue$': 'vue-jest'
55
}

0 commit comments

Comments
 (0)