Skip to content

Commit 50ca8a8

Browse files
committed
Merge remote-tracking branch 'origin/master' into chiubaca-branch
2 parents 91ebd2b + 16d104c commit 50ca8a8

File tree

2 files changed

+64
-29
lines changed

2 files changed

+64
-29
lines changed

README.md

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Cheatsheets for experienced Vue developers getting started with TypeScript.
44

5+
- [Vue 3 specifics](vue-3.md)
6+
57
# Section 1: Setup
68

79
## Prerequisites
@@ -11,53 +13,54 @@ Cheatsheets for experienced Vue developers getting started with TypeScript.
1113

1214
## Vue + TypeScript Starter Kits
1315

14-
1. Using the [Vue CLI](https://vuejs.org/v2/guide/installation.html#CLI) , you can select the [TypeScript plugin](https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-typescript) to be setup in a new a Vue project.
16+
1. Using the [Vue CLI](https://vuejs.org/v2/guide/installation.html#CLI) , you can select the [TypeScript plugin](https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-typescript) to be setup in a new a Vue project.
1517

16-
```bash
17-
# 1. Install Vue CLI, if it's not already installed
18-
npm install --global @vue/cli
18+
```bash
19+
# 1. Install Vue CLI, if it's not already installed
20+
npm install --global @vue/cli
1921

20-
# 2. Create a new project, then choose the "Manually select features" option
21-
vue create <my-project-name>
22-
```
22+
# 2. Create a new project, then choose the "Manually select features" option
23+
vue create <my-project-name>
24+
```
2325

2426
2. [Vite](https://github.com/vitejs/vite) is a new build tool by Evan You. It currently only works with Vue 3.x but supports TypeScript out-of-the-box.
2527

26-
> ⚠ Currently in beta. Do not use in production.
28+
> ⚠ Currently in beta. Do not use in production.
2729
28-
```bash
29-
npm init vite-app <project-name>
30-
cd <project-name>
31-
npm install
32-
npm run dev
33-
```
30+
```bash
31+
npm init vite-app <project-name>
32+
cd <project-name>
33+
npm install
34+
npm run dev
35+
```
3436

3537
# Section 2: Getting Started
3638

3739
## Recommended `ts.config` setup
3840

39-
>note: `strict:true` stricter inference for data properties on `this`. If you do not use it, `this` will always be treated as `any`
41+
> note: `strict:true` stricter inference for data properties on `this`. If you do not use it, `this` will always be treated as `any`
42+
4043
```json
4144
// tsconfig.json
4245
{
43-
"compilerOptions": {
44-
"target": "esnext",
45-
"module": "esnext",
46-
"strict": true,
47-
"moduleResolution": "node"
48-
}
46+
"compilerOptions": {
47+
"target": "esnext",
48+
"module": "esnext",
49+
"strict": true,
50+
"moduleResolution": "node"
51+
}
4952
}
5053
```
5154

5255
## Usage in `.vue` files
56+
5357
Add `lang="ts"` to the script tag to declare TS as the `lang` used.
58+
5459
```js
55-
<script lang="ts">
56-
...
57-
</script>
60+
<script lang='ts'>...</script>
5861
```
5962

60-
In Vue 2.x you need to define components with `Vue.component` or `Vue.extend`:
63+
In Vue 2.x you need to define components with `Vue.component` or `Vue.extend`:
6164

6265
```js
6366
<script lang="ts">
@@ -77,11 +80,11 @@ export default Vue.extend({
7780
In Vue 3.x you can use `defineComponent` to get type inference in Vue component options
7881

7982
```js
80-
import { defineComponent } from 'vue'
83+
import { defineComponent } from 'vue';
8184

8285
const Component = defineComponent({
83-
// type inference enabled
84-
})
86+
// type inference enabled
87+
});
8588
```
8689

8790
### Class Components
@@ -197,4 +200,4 @@ export default class InfoCard extends Vue {
197200

198201
# Other Vue + TypeScript resources
199202
- Views on Vue podcast - https://devchat.tv/views-on-vue/vov-076-typescript-tell-all-with-jack-koppa/
200-
- Focuses a lot on class components and vue-property-decorator - https://blog.logrocket.com/how-to-write-a-vue-js-app-completely-in-typescript/
203+
- Focuses a lot on class components and vue-property-decorator - https://blog.logrocket.com/how-to-write-a-vue-js-app-completely-in-typescript/

vue-3.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This document is for Vue-3 _specific_ syntax
2+
3+
### Setup Template Attribute Syntax
4+
5+
⚠️ This feature is still a WIP, and is an active RFC. [Follow the conversation](https://github.com/vuejs/rfcs/pull/182) to stay up to date with the lifecycle.
6+
7+
This provides a better DX when using the setup function in Vue SFCs.
8+
Type inference still works, and you don't need to use `defineComponent`..
9+
It's important to note that this is just **syntactic sugar**, and still get's compiled down to a component
10+
using `defineComponent` and the `setup` function.
11+
12+
```vue
13+
<script setup lang="ts">
14+
import { ref } from 'vue';
15+
16+
export const welcome = ref('Welcome to Vue 3 with TS!');
17+
</script>
18+
```
19+
20+
`props` and the `context` object work as well.
21+
For TS inference, declare them using TS syntax.
22+
23+
```vue
24+
<script setup="props, ctx" lang="ts">
25+
import { computed } from 'vue'
26+
27+
declare const props: {
28+
name: string
29+
}
30+
31+
export const welcome = computed(() => `Welcome, ${props.name}!`)
32+
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy