Vue 3 with Typescript – 1 to 3

Part 1

ref – https://www.youtube.com/watch?v=JfI5PISLr9w

Why Use Typescript
– type errors are caught at compile time
– easier to read the code
– better debugging

vue create hyrule-jobs

Use space bar to select manually select features.

Want to use TypeScript
use 3.x
DO NOT want class style component syntax
DO want to use Babel
Pick linter: Basic
Lint on Save
In dedicated config files

cd hyrule-jobs

Code . to open it with Visual Studio Code

npm run serve

File layout

Open the src folder.
notice main.ts

It is a typescript file.

shims-vue.d.ts – tells typescript all about vue files so it can better understand single file components
don’t change any of it.

tsconfig.json – typescript compiler

Look at src/App.vue
script tag has lang attribute for ts. That’s because we’re using typescript inside of it.

Go into components, delete HelloWorld.vue

Go into assets, delete logo.png

In App.vue, remove all code in template tag. Leave a div with text ‘Link’ in it.
remove HelloWorld import
remove HelloWorld from components

remove styles from style tags.

App.vue

create assets > global.css

Register css in main.ts

npm run serve

You will see text.

Part 2

ref – https://www.youtube.com/watch?v=UDAVj_vlAr8

Declare own data ‘name’ and display it in our template:

App.vue

create methods to set name:

Test by running browser again.

Using Union Types

For data property, we must use type casting. If it was a single type, it gets inferred. But what if we want to use multiple type such as number OR string?

create changeAge method:

duplicate button, and change it for age. Use number 30 for the parameter. Can also use string “30” for the parameter.

Part 3

ref – https://www.youtube.com/watch?v=H-hXNym2CK8

https://stackoverflow.com/questions/61452458/ref-vs-reactive-in-vue-3

  • reactive() only takes objects, NOT JS primitives (String, Boolean, Number, BigInt, Symbol, null, undefined)
  • ref() is calling reactive() behind the scenes
  • Since reactive() works for objects and ref() calls reactive(), objects work for both BUT, ref() has a .value property for reassigning, reactive() does not have this and therefore CANNOT be reassigned

therefore…

Use ref() when..

  • it’s a primitive (for example ‘string’, true, 23, etc)
  • it’s an object you need to later reassign (like an array)

ref() is good for objects that need to be reassigned, like an array.

If we were to do the same with reactive, we’d have to reassign property posts

reactive() Use-Case
A good use-case for reactive() is a group of primitives that belong together:

the code above feels more logical than

Let’s try grouping name and age in an object and passing it to reactive. Notice that its more natural when we use the variable state? Even better if we change it to personState, because properties name and age group together better.

We then set properties like so:

using refs

Create custom type

create folder called types in src
in types folder, create jobs.ts

job.ts

use it inside App component

src/App.vue

Check the output