-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Conversation
There was a problem hiding this 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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 😛
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 😅
It looks so great to me! 😄 |
@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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great works! 😄
@chrisvfritz Looks great! Thanks for your revising 😄 |
@STayinloves It's all a lie, unfortunately. @phanan photoshopped a screenshot I took to better match Vue's colors. 🙂 |
😛 I'm sorry… |
Oh, anyway, still thank both of you. Maybe can hack one some day. @chrisvfritz @phanan |
I write a color theme like this for VSCode today. Published at here. Someone may have interest.😛 |
* 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
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 :)