- Notifications
You must be signed in to change notification settings - Fork17
Cheatsheets for experienced Vue developers getting started with TypeScript
License
typescript-cheatsheets/vue
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Cheatsheets for experienced Vue developers getting started with TypeScript.
- 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>
- 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
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" }}
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});
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>
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.
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},}})
- Views on Vue podcast -https://devchat.tv/views-on-vue/vov-076-typescript-tell-all-with-jack-koppa/
- 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 Hooks and Type Safety with TypeScript -https://www.youtube.com/watch?v=aJdi-uEKYAc
About
Cheatsheets for experienced Vue developers getting started with TypeScript
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Contributors3
Uh oh!
There was an error while loading.Please reload this page.