You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#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.
Copy file name to clipboardexpand all lines: README.md
+112-3
Original file line number
Diff line number
Diff line change
@@ -1179,9 +1179,6 @@ vue create hello-vue3
1179
1179
1180
1180
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.
1181
1181
1182
-
1183
-
1184
-
1185
1182
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).
1186
1183
1187
1184
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
1255
1252
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.
1256
1253
1257
1254
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:
? 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:
1258
1336
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):
0 commit comments