Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Cheatsheets for experienced Vue developers getting started with TypeScript

License

NotificationsYou must be signed in to change notification settings

typescript-cheatsheets/vue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Cheatsheets for experienced Vue developers getting started with TypeScript.

Section 1: Setup

Prerequisites

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

Vue + TypeScript Starter Kits

  1. Using theVue CLI , you can select theTypeScript plugin to be setup in a new a Vue project.
# 1. Install Vue CLI, if it's not already installednpm install --global @vue/cli# 2. Create a new project, then choose the "Manually select features" optionvue 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 installnpm run dev

Section 2: Getting Started

Recommendedts.config setup

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

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

Usage in.vue files

Addlang="ts" to the script tag to declare TS as thelang used.

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

In Vue 2.x you need to define components withVue.component orVue.extend:

<scriptlang="ts">import Vue from "vue";export default Vue.extend({// type inference enabledname:"HelloWorld",props:{msg:String}});</script>

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

import{defineComponent}from'vue';constComponent=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">importVuefrom"vue";interfacePersonInfo {  firstName:string,  surname:string,  age:number}exportdefaultVue.extend({  name:"InfoCard",  props: {    info: {      type:ObjectasPropType<PersonInfo>,      required:true    }  }});</script>

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

import Vue from 'vue'<script lang="ts">importVuefrom"vue";interfacePersonInfo {  firstName:string,  surname:string,  age:number}exportdefaultVue.extend({  name:"InfoCard",  props: {    info: {      type:Objectas ()=>PersonInfo,      required:true    }  }});</script>

Data Properties (Options API)

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

interfacePost{title:string;contents:string;likes:number;}exportdefaultVue.extend({data():{newPost:Post}{return{newPost:{title:"",contents:"",likes:0}};}});

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

interfacePost{title:string;contents:string;likes:number;}exportdefaultVue.extend({data(){return{newPost:{title:"",contents:"",likes:0}asPost// ❌ Avoid doing this};}});

Note thattype assertion like this does not provide any type safety. If for example, thecontents property was missing innewPost, TypeScript would not catch this error.

Computed Properties (Options API)

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

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

Other Vue + TypeScript resources

About

Cheatsheets for experienced Vue developers getting started with TypeScript

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp