Skip to content

Commit d3ce5ae

Browse files
authored
Merge pull request #3 from typescript-cheatsheets/chiubaca-getting-started
2 parents 11e60c5 + 001cc68 commit d3ce5ae

File tree

1 file changed

+59
-8
lines changed

1 file changed

+59
-8
lines changed

README.md

+59-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
1-
# vue-typescript-cheatsheet
1+
# Vue+TypeScript Cheatsheets
22

33
Cheatsheets for experienced Vue developers getting started with TypeScript
44

5-
- https://devchat.tv/views-on-vue/vov-076-typescript-tell-all-with-jack-koppa/
5+
# Section 1: Setup
66

7+
## Prerequisites
78

8-
## Recommended ts.config setup
9-
note: `strict:true` stricter inference for data properties on `this`. If you do not use it, `this` will always be treated as `any`
9+
1. A good understanding of [Vue.js](https://vuejs.org/)
10+
2. Read the TypeScript support section in the Vue docs [2.x](https://vuejs.org/v2/guide/typescript.html) | [3.x](https://v3.vuejs.org/guide/typescript-support.html#typescript-support)
11+
12+
## Vue + TypeScript Starter Kits
13+
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.
15+
16+
```bash
17+
# 1. Install Vue CLI, if it's not already installed
18+
npm install --global @vue/cli
19+
20+
# 2. Create a new project, then choose the "Manually select features" option
21+
vue create <my-project-name>
22+
```
23+
24+
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.
25+
26+
> ⚠ Currently in beta. Do not use in production.
27+
28+
```bash
29+
npm init vite-app <project-name>
30+
cd <project-name>
31+
npm install
32+
npm run dev
33+
```
34+
35+
# Section 2: Getting Started
36+
37+
## Recommended `ts.config` setup
38+
39+
>note: `strict:true` stricter inference for data properties on `this`. If you do not use it, `this` will always be treated as `any`
1040
```json
1141
// tsconfig.json
1242
{
@@ -19,19 +49,40 @@ note: `strict:true` stricter inference for data properties on `this`. If you do
1949
}
2050
```
2151

22-
## Usage in .vue files
23-
add `lang="ts"` to the script tag to declare TS as the lang used.
24-
```html
52+
## Usage in `.vue` files
53+
Add `lang="ts"` to the script tag to declare TS as the `lang` used.
54+
```js
2555
<script lang="ts">
2656
...
2757
</script>
2858
```
2959

30-
use `defineComponent` to get type inference in Vue component options
60+
In Vue 2.x you need to define components with `Vue.component` or `Vue.extend`:
61+
62+
```js
63+
<script lang="ts">
64+
import Vue from "vue";
65+
66+
export default Vue.extend({
67+
68+
// type inference enabled
69+
name: "HelloWorld",
70+
props: {
71+
msg: String
72+
}
73+
});
74+
</script>
75+
```
76+
77+
In Vue 3.x you can use `defineComponent` to get type inference in Vue component options
78+
3179
```js
3280
import { defineComponent } from 'vue'
3381

3482
const Component = defineComponent({
3583
// type inference enabled
3684
})
3785
```
86+
87+
# Other Vue + TypeScript resources
88+
- Views on Vue podcast - https://devchat.tv/views-on-vue/vov-076-typescript-tell-all-with-jack-koppa/

0 commit comments

Comments
 (0)