typescript-cheatsheets / vue

Licence: MIT License
Cheatsheets for experienced Vue developers getting started with TypeScript

Projects that are alternatives of or similar to vue

Kb
A minimalist command line knowledge base manager
Stars: ✭ 2,789 (+1367.89%)
Mutual labels:  cheatsheets
almanacs
A recipe for everything 🗒️
Stars: ✭ 47 (-75.26%)
Mutual labels:  cheatsheets
ds
👨‍🔬 In Russian: Обновляемая структурированная подборка бесплатных ресурсов по тематикам Data Science: курсы, книги, открытые данные, блоги и готовые решения.
Stars: ✭ 102 (-46.32%)
Mutual labels:  cheatsheets
csaoid
Cheat Sheets and Other Interesting Documents
Stars: ✭ 20 (-89.47%)
Mutual labels:  cheatsheets
systems-programming-cheat-sheet
Cheat sheet for x86-64 Unix systems programming
Stars: ✭ 328 (+72.63%)
Mutual labels:  cheatsheets
memo-dev
Knowledge base, Today I Learned, Cheatsheet ... Call this as you want ...
Stars: ✭ 13 (-93.16%)
Mutual labels:  cheatsheets
Ml Glossary
Machine learning glossary
Stars: ✭ 2,338 (+1130.53%)
Mutual labels:  cheatsheets
cheatsheet
📝 A Repository for developers for different kinds of Programming Cheatsheets
Stars: ✭ 217 (+14.21%)
Mutual labels:  cheatsheets
html-cheatsheet
See the basic syntax of HTML and the terminologies around it
Stars: ✭ 146 (-23.16%)
Mutual labels:  cheatsheets
cheat-sheet
collection of cheat sheets
Stars: ✭ 150 (-21.05%)
Mutual labels:  cheatsheets
cheatsheet-python-A4
📖 Advanced Python Syntax In A4
Stars: ✭ 96 (-49.47%)
Mutual labels:  cheatsheets
Asciidots-Cheat-Sheet
My personal Asciidots Cheat Sheet in .jpg .odt .pdf .png and obviously in .txt
Stars: ✭ 17 (-91.05%)
Mutual labels:  cheatsheets
nus-notes-cheatsheets
Notes and cheatsheets from NUS modules taken as part of the Computer Science curriculum.
Stars: ✭ 97 (-48.95%)
Mutual labels:  cheatsheets
LearningResources
A centralised hub for learner around the globe from A-Z. You can find collections of manuals, blogs, hacks, one liners, courses, other free learning-resources and more
Stars: ✭ 63 (-66.84%)
Mutual labels:  cheatsheets
css-cheatsheet
Syntax, terminologies, selectors, and common properties of CSS
Stars: ✭ 114 (-40%)
Mutual labels:  cheatsheets
Javascripter
Helping junior developers navigate the complex world of software engineering without experiencing information overload.
Stars: ✭ 203 (+6.84%)
Mutual labels:  cheatsheets
dev-cheatsheets
A collection of code snippets and CLI guides for quick and easy reference while coding
Stars: ✭ 33 (-82.63%)
Mutual labels:  cheatsheets
Awesome-Math-Learning
📜 Collection of the most awesome Math learning resources in the form of notes, videos and cheatsheets.
Stars: ✭ 73 (-61.58%)
Mutual labels:  cheatsheets
Cyanhall
Content List on Cyanhall, Star me if it’s useful ★ Cheatsheets
Stars: ✭ 39 (-79.47%)
Mutual labels:  cheatsheets
react-cheatsheets
Create and generate cheat sheets using React
Stars: ✭ 21 (-88.95%)
Mutual labels:  cheatsheets

Vue+TypeScript Cheatsheets

Cheatsheets for experienced Vue developers getting started with TypeScript.

Section 1: Setup

Prerequisites

  1. A good understanding of Vue.js
  2. Read the TypeScript support section in the Vue docs 2.x | 3.x

Vue + TypeScript Starter Kits

  1. Using the Vue CLI , you can select the TypeScript plugin to be setup in a new a Vue project.
# 1. Install Vue CLI, if it's not already installed
npm install --global @vue/cli

# 2. Create a new project, then choose the "Manually select features" option
vue create <my-project-name>
  1. Vite is a new build tool by Evan You. It currently only works with Vue 3.x but supports TypeScript out-of-the-box.

Currently in beta. Do not use in production.

npm init vite-app <project-name>
cd <project-name>
npm install
npm run dev

Section 2: Getting Started

Recommended ts.config setup

note: strict:true stricter inference for data properties on this. If you do not use it, this will always be treated as any

// tsconfig.json
{
    "compilerOptions": {
        "target": "esnext",
        "module": "esnext",
        "strict": true,
        "moduleResolution": "node"
    }
}

Usage in .vue files

Add lang="ts" to the script tag to declare TS as the lang used.

<script lang='ts'>...</script>

In Vue 2.x you need to define components with Vue.component or Vue.extend:

<script lang="ts">
import Vue from "vue";

export default Vue.extend({

  // type inference enabled
  name: "HelloWorld",
  props: {
    msg: String
  }
});
</script>

In Vue 3.x you can use defineComponent to get type inference in Vue component options

import { defineComponent } from 'vue';

const Component = defineComponent({
    // type inference enabled
});

Props

PropType can be used to annotate props with a particular object shape.

import Vue, { PropType } from 'vue'

<script lang="ts">
import Vue from "vue";

interface PersonInfo { 
  firstName: string,
  surname: string,
  age: number
}

export default Vue.extend({
  
  name: "InfoCard",
  props: {
    info: {
      type: Object as PropType<PersonInfo>,
      required: true
    }
  }
});
</script>

Alternatively, you can also annote your prop types with an anonymous function:

import Vue from 'vue'

<script lang="ts">
import Vue from "vue";

interface PersonInfo { 
  firstName: string,
  surname: string,
  age: number
}

export default Vue.extend({
  
  name: "InfoCard",
  props: {
    info: {
      type: Object as () => PersonInfo,
      required: true
    }
  }
});
</script>

Data Properties (Options API)

You can enforce types on Vue data properties by annotating the return data object:

interface Post {
  title: string;
  contents: string;
  likes: number;
}

export default Vue.extend({
  data(): { newPost: Post } {
    return {
      newPost: {
        title: "",
        contents: "",
        likes: 0
      }
    };
  }
});

It might be tempting to annotate your Vue data properties using as like this:

interface Post {
  title: string;
  contents: string;
  likes: number;
}

export default Vue.extend({
  data() {
    return {
      newPost: {
        title: "",
        contents: "",
        likes: 0
      } as Post // ❌ Avoid doing this
    };
  }
});

Note that type assertion like this does not provide any type safety. If for example, the contents property was missing in newPost, TypeScript would not catch this error.

Computed Properties (Options API)

Typing the return type for your computed properties is important especially when this is involved as TypeScript sometimes has trouble infering the type.

export default Vue.extend({
  data() {
    return {
      name: 'World',
    }
  },
  computed: {
    greet(): string {  //👈 Remember to annotate your computed properties like so. 
      return 'Hello ' + this.name
    },
  }
})

Other Vue + TypeScript resources

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].