{"id":19580115,"url":"https://github.com/typescript-cheatsheets/vue","last_synced_at":"2025-04-10T03:56:36.094Z","repository":{"id":41065646,"uuid":"184504345","full_name":"typescript-cheatsheets/vue","owner":"typescript-cheatsheets","description":"Cheatsheets for experienced Vue developers getting started with TypeScript","archived":false,"fork":false,"pushed_at":"2022-04-30T23:06:16.000Z","size":28,"stargazers_count":247,"open_issues_count":4,"forks_count":16,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-30T01:25:44.506Z","etag":null,"topics":["cheatsheets","typescript","vue"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/typescript-cheatsheets.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-02T01:25:20.000Z","updated_at":"2025-02-25T11:32:17.000Z","dependencies_parsed_at":"2022-09-09T17:52:55.343Z","dependency_job_id":null,"html_url":"https://github.com/typescript-cheatsheets/vue","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typescript-cheatsheets%2Fvue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typescript-cheatsheets%2Fvue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typescript-cheatsheets%2Fvue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typescript-cheatsheets%2Fvue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/typescript-cheatsheets","download_url":"https://codeload.github.com/typescript-cheatsheets/vue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246719260,"owners_count":20822708,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cheatsheets","typescript","vue"],"created_at":"2024-11-11T07:22:18.246Z","updated_at":"2025-04-03T01:08:44.314Z","avatar_url":"https://github.com/typescript-cheatsheets.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Vue+TypeScript Cheatsheets\n\nCheatsheets for experienced Vue developers getting started with TypeScript.\n\n-   [Vue 3 specifics](vue-3.md)\n-   [Class Components \u0026 Decorators](class-components.md)\n\n# Section 1: Setup\n\n## Prerequisites\n\n1. A good understanding of [Vue.js](https://vuejs.org/)\n2. 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)\n\n## Vue + TypeScript Starter Kits\n\n1. 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.\n\n```bash\n# 1. Install Vue CLI, if it's not already installed\nnpm install --global @vue/cli\n\n# 2. Create a new project, then choose the \"Manually select features\" option\nvue create \u003cmy-project-name\u003e\n```\n\n2. [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.\n\n\u003e ⚠ Currently in beta. Do not use in production.\n\n```bash\nnpm init vite-app \u003cproject-name\u003e\ncd \u003cproject-name\u003e\nnpm install\nnpm run dev\n```\n\n# Section 2: Getting Started\n\n## Recommended `ts.config` setup\n\n\u003e note: `strict:true` stricter inference for data properties on `this`. If you do not use it, `this` will always be treated as `any`\n\n```json\n// tsconfig.json\n{\n    \"compilerOptions\": {\n        \"target\": \"esnext\",\n        \"module\": \"esnext\",\n        \"strict\": true,\n        \"moduleResolution\": \"node\"\n    }\n}\n```\n\n## Usage in `.vue` files\n\nAdd `lang=\"ts\"` to the script tag to declare TS as the `lang` used.\n\n```js\n\u003cscript lang='ts'\u003e...\u003c/script\u003e\n```\n\nIn Vue 2.x you need to define components with `Vue.component` or `Vue.extend`:\n\n```js\n\u003cscript lang=\"ts\"\u003e\nimport Vue from \"vue\";\n\nexport default Vue.extend({\n\n  // type inference enabled\n  name: \"HelloWorld\",\n  props: {\n    msg: String\n  }\n});\n\u003c/script\u003e\n```\n\nIn Vue 3.x you can use `defineComponent` to get type inference in Vue component options\n\n```js\nimport { defineComponent } from 'vue';\n\nconst Component = defineComponent({\n    // type inference enabled\n});\n```\n\n## Props\n\n`PropType` can be used to annotate props with a particular object shape.\n\n```vue\nimport Vue, { PropType } from 'vue'\n\n\u003cscript lang=\"ts\"\u003e\nimport Vue from \"vue\";\n\ninterface PersonInfo { \n  firstName: string,\n  surname: string,\n  age: number\n}\n\nexport default Vue.extend({\n  \n  name: \"InfoCard\",\n  props: {\n    info: {\n      type: Object as PropType\u003cPersonInfo\u003e,\n      required: true\n    }\n  }\n});\n\u003c/script\u003e\n```\n\nAlternatively, you can also annote your prop types with an anonymous function:\n\n```vue\nimport Vue from 'vue'\n\n\u003cscript lang=\"ts\"\u003e\nimport Vue from \"vue\";\n\ninterface PersonInfo { \n  firstName: string,\n  surname: string,\n  age: number\n}\n\nexport default Vue.extend({\n  \n  name: \"InfoCard\",\n  props: {\n    info: {\n      type: Object as () =\u003e PersonInfo,\n      required: true\n    }\n  }\n});\n\u003c/script\u003e\n```\n\n## Data Properties (Options API)\n\nYou can enforce types on Vue data properties by annotating the return data object:\n\n```ts\ninterface Post {\n  title: string;\n  contents: string;\n  likes: number;\n}\n\nexport default Vue.extend({\n  data(): { newPost: Post } {\n    return {\n      newPost: {\n        title: \"\",\n        contents: \"\",\n        likes: 0\n      }\n    };\n  }\n});\n```\n\nIt might be tempting to annotate your Vue data properties using `as` like this:\n\n```ts\ninterface Post {\n  title: string;\n  contents: string;\n  likes: number;\n}\n\nexport default Vue.extend({\n  data() {\n    return {\n      newPost: {\n        title: \"\",\n        contents: \"\",\n        likes: 0\n      } as Post // ❌ Avoid doing this\n    };\n  }\n});\n```\nNote that [type assertion](https://www.typescriptlang.org/docs/handbook/basic-types.html#type-assertions) like this does not provide any type safety. If for example, the `contents` property was missing in `newPost`, TypeScript would not catch this error. \n\n## Computed Properties (Options API)\n\nTyping the return type for your computed properties is important especially when `this` is involved as TypeScript sometimes has trouble infering the type. \n\n```ts\n\nexport default Vue.extend({\n  data() {\n    return {\n      name: 'World',\n    }\n  },\n  computed: {\n    greet(): string {  //👈 Remember to annotate your computed properties like so. \n      return 'Hello ' + this.name\n    },\n  }\n})\n\n```\n\n\u003e \n\n\n# Other Vue + TypeScript resources\n- Views on Vue podcast - https://devchat.tv/views-on-vue/vov-076-typescript-tell-all-with-jack-koppa/\n- Focuses a lot on class components and vue-property-decorator - https://blog.logrocket.com/how-to-write-a-vue-js-app-completely-in-typescript/\n- Vue 3 Hooks and Type Safety with TypeScript - https://www.youtube.com/watch?v=aJdi-uEKYAc\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypescript-cheatsheets%2Fvue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftypescript-cheatsheets%2Fvue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypescript-cheatsheets%2Fvue/lists"}