Skip to content
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

Update typescript.md to describe how to use typescript types without module bundler (closes #2042) #2137

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/v2/guide/typescript.md
Expand Up @@ -70,6 +70,18 @@ const Component = {
}
```

To use Vue types in an environment without a module bundler, you can create a custom type definition file containing `declare global` (see [Declaration Merging https://www.typescriptlang.org/docs/handbook/declaration-merging.html](https://www.typescriptlang.org/docs/handbook/declaration-merging.html)).
Copy link
Member

Choose a reason for hiding this comment

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

It would make more sense to explicitly describe what the definition file does.

To use Vue types

I feel we cannot use Vue's types (like ComponentOptions etc.) without this definition which is not correct from this phrase.

What about the below change?

Suggested change
To use Vue types in an environment without a module bundler, you can create a custom type definition file containing `declare global` (see [Declaration Merging https://www.typescriptlang.org/docs/handbook/declaration-merging.html](https://www.typescriptlang.org/docs/handbook/declaration-merging.html)).
To use Vue in an environment without a module bundler, you can create a custom type definition file containing `declare global` to declare global `Vue` object type (see [Declaration Merging https://www.typescriptlang.org/docs/handbook/declaration-merging.html](https://www.typescriptlang.org/docs/handbook/declaration-merging.html)).

Copy link
Author

Choose a reason for hiding this comment

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

Thank you all for reviewing. Yes, wording needs some work to make it clear what is the purpose of this. You can use Vue fine in typescript without importing types or creating custom type definition file with declare global if you can live with errors (javascript still gets generated correctly) and without intellisense and type recognition in IDE.

Copy link
Author

@realmerx realmerx Apr 23, 2019

Choose a reason for hiding this comment

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

Better wording very welcome but here is my try.

Suggested change
To use Vue types in an environment without a module bundler, you can create a custom type definition file containing `declare global` (see [Declaration Merging https://www.typescriptlang.org/docs/handbook/declaration-merging.html](https://www.typescriptlang.org/docs/handbook/declaration-merging.html)).
Vue typescript types normally need to be imported (to get error free typescript compiling and intellisense in IDE). This would require environment with module bundler. To get them working without module bundler one can create custom type definition file containing `declare global` to declare global `Vue` object type (see [Declaration Merging https://www.typescriptlang.org/docs/handbook/declaration-merging.html](https://www.typescriptlang.org/docs/handbook/declaration-merging.html)).

Copy link
Member

Choose a reason for hiding this comment

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

Wait, is this code really work on your environment? I just try it in my local but there is a compile error on it. I'm using the latest Vue (v2.6.10).

Subsequent variable declarations must have the same type.  Variable 'Vue' must be of type 'typeof import("(...)/node_modules/vue/types/index")', but here has type 'VueConstructor<Vue>'.ts(2403)

Copy link
Member

Choose a reason for hiding this comment

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

OK, I realized there is already this kind of type declaration in Vue's typings. https://github.com/vuejs/vue/blob/dev/types/index.d.ts#L5

But this declaration is actually not correct as Vue object type is declared under Vue.default. This causes the compilation error which I showed above.

I already created the PR to fix this bug. Once this is merged, you can use global Vue without any additional set up which means we would no longer require this section in docs.
vuejs/vue#9912

Copy link
Author

Choose a reason for hiding this comment

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

Yes I think something has changed in latest typescript version that makes this kind of declaration break - I need to investigate more. But if we can make it work in global context without tricks then even better.


``` ts
//global.d.ts - name does not matter
import { VueConstructor } from 'vue'

declare global {
const Vue: VueConstructor
}
```


## Class-Style Vue Components

If you prefer a class-based API when declaring components, you can use the officially maintained [vue-class-component](https://github.com/vuejs/vue-class-component) decorator:
Expand Down