Skip to content

Add Vue + TypeScript guide #649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 14, 2016
Merged

Add Vue + TypeScript guide #649

merged 7 commits into from
Dec 14, 2016

Conversation

ktsn
Copy link
Member

@ktsn ktsn commented Dec 12, 2016

I've wrote a beginner's guide of using Vue.js with TypeScript. It introduces official declaration files and vue-class-component briefly. I think readers can make simple applications after understanding this guide.

I added this guide after SSR guide but not sure it is appropriate place.

@kaorun343 @HerringtonDarkholme Your review would be appreciated if you have time :)

Copy link
Member

@HerringtonDarkholme HerringtonDarkholme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than some grammar issues, this guide looks great!


## Official Declaration Files

There are official declaration files for Vue, vue-router and vuex. You do not have to use external tools like `typings` to use them since they are published via npm. You can install Vue's declarations with `npm install vue`. Then, all declarations can be imported with `import` statement.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe consistent case is better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that Vue is title case but vue-router and vuex is following github repository name in the docs.
Is that correct? /ping @chrisvfritz

Example of component definition with TypeScript:

``` ts
// we cannot write `import Vue from 'vue'` in TypeScript

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks for pointing this out! Importing is a pain point I have seen a lot in TS community.

Copy link

@ryutamaki ryutamaki Dec 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use import * as Vue from "vue"; this syntax instead (at least, working well to me).
I don't sure but I'm using webpack + ts-loader + vue-loader and these might affect my module structure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryutamaki
Actually, import * as Vue from "vue"; and import Vue = require("vue"); are the same thing. I choose the later syntax because it would be more intentional to import single object.

But maybe writing the former syntax as a note would also be good. I'll add about that, thanks for pointing this out!

Copy link
Member

@HerringtonDarkholme HerringtonDarkholme Dec 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel ambivalent toward import * as Vue from "vue" syntax. On one hand, it is a standard syntax for importing. On the other hand, TypeScript's runtime behavior to consume commonjs module is different from other transpilers like Babel: Babel user will write import Vue from "vue", but TSC user will write import * as Vue.

TypeScript's behavior is also incompatible with node-eps. I think choosing the nonstandard syntax is a good sign to user that module system is still not stable.

@DanielRosenwasser, I would be honored if you can give suggestion on this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'd say that import Vue = require("vue"); is the most appropriate way to import a CommonJS module given that the interop behavior hasn't been settled on yet.

})
```

Note that `Vue` is already typed, so all methods, properties and parameters will be type checked. For example, if you typo `template` option, TypeScript compiler will print an error message on a compile time.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems typo is a noun. https://www.merriam-webster.com/dictionary/typo
Would you please change it to if you have a typo in template option ?


You can see all available types on the corresponding repository ([here are Vue's types](https://github.com/vuejs/vue/blob/dev/types/index.d.ts)).

<p class="tip">The declaration files of Vue requires `--lib DOM,ES2015` as compiler option. So you need to pass the option via `tsc` command or `tsconfig.json`. For more details of compiler options, please see [Compiler Options · TypeScript](https://www.typescriptlang.org/docs/handbook/compiler-options.html)</p>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to require only --lib DOM,ES2015.promise. Because Promise is the only requirements in Vue(and it is optional, if users do not use async component.). IMHO, listing only API we used is in accordance with the rule of least power.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is definitely better 🙂

order: 25
---

If you develop a large application, using a language having static type checking would be helpful. If your code seems to cause unexpected behavior, it provides useful errors before you run it on browsers. You can easily use [TypeScript](https://www.typescriptlang.org/) (one of popular AltJS having static type checking) with Vue since Vue officially provides its declaration files.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If your code seems to cause unexpected behavior

This sentence does not make much sense to me.... Maybe If you have unexpected typos in your code is better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it would be clearer and understandable. So how about For example, if you have typos in your code, ... ? Because typos are always unexpected thing 😛

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have unexpected mistakes in your code?

// all component options are allowed in here
template: '<button @click="onClick">Click!</button>'
})
export default MyComponent extends Vue {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class is missing

export default class MyComponent extends Vue

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I often miss it 😅

@kaorun343
Copy link
Contributor

It looks so great to me! 😄

@chrisvfritz
Copy link
Contributor

chrisvfritz commented Dec 14, 2016

@ktsn @kaorun343 @HerringtonDarkholme I just revised this and would like your feedback to make sure I didn't ruin it. 😄 Fantastic info here! Beyond tweaking some language and reorganizing a bit, I removed some explanations about TypeScript that weren't specific to Vue (e.g. different ways of importing JS modules), as I think we might want to assume some TypeScript experience here. I'm not an expert though, so let me know if you think that was a mistake.


## Official Declaration Files

A strong type system can help prevent many potential runtime errors, especially as applications grow. That's why Vue ships with [official type declarations](https://github.com/vuejs/vue/tree/dev/types) for [TypeScript](https://www.typescriptlang.org/) - not only in Vue core, but also [for Vue Router](https://github.com/vuejs/vue-router/tree/dev/types) and [for Vuex](https://github.com/vuejs/vuex/tree/dev/types) as well.
Copy link
Member

@HerringtonDarkholme HerringtonDarkholme Dec 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind replacing strong with static? TypeScript isn't type sound in language design. static is probably a better choice to express compile time checking.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Member

@HerringtonDarkholme HerringtonDarkholme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great works! 😄

@ktsn
Copy link
Member Author

ktsn commented Dec 14, 2016

@chrisvfritz Looks great! Thanks for your revising 😄

@chrisvfritz chrisvfritz merged commit dbdf1ca into vuejs:master Dec 14, 2016
@ktsn ktsn deleted the typescript branch December 14, 2016 07:39
@sanxing-chen
Copy link

Could you please to tell me which color theme do you use in the VScode? Thank you.
pic

@chrisvfritz
Copy link
Contributor

@STayinloves It's all a lie, unfortunately. @phanan photoshopped a screenshot I took to better match Vue's colors. 🙂

@phanan
Copy link
Member

phanan commented Jan 14, 2017

😛 I'm sorry…

@sanxing-chen
Copy link

Oh, anyway, still thank both of you. Maybe can hack one some day. @chrisvfritz @phanan

@sanxing-chen
Copy link

I write a color theme like this for VSCode today. Published at here. Someone may have interest.😛

kazupon pushed a commit to kazupon/vuejs.org that referenced this pull request Oct 1, 2017
* add vue + typescript guide

* add introduction of typescript guide

* add a caveat of official declaration files

* improve typescript guide by reviews

* fix the description of typescript again

* revise and reorganize typescript doc

* Update strong to static in typescript guide
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants