-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
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.
I'll need another team member to review this, as I myself don't use TS.
…out module bundler grammar fixes by phanan Co-Authored-By: realmerx <realmerx@gmail.com>
src/v2/guide/typescript.md
Outdated
@@ -70,7 +70,8 @@ const Component = { | |||
} | |||
``` | |||
|
|||
To use vue types in environment without module bundler you can create custom type definition file containing declare global (see https://www.typescriptlang.org/docs/handbook/declaration-merging.html) | |||
To use Vue types in an environment without a module bundler, you can create a custom type definition file containing `declared global` (see [Declaration Merging |
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.
The markup is broken here.
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.
Not sure I understood exactly what is broken in markup? Found a typo and added link to url label as well (if it conflicts with style then we can of course keep just the name in url label).
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.
Preview your markdown and you'll see what I mean.
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.
Interesting. I am probably missing something obvious because I did preview and looks correct to me. Here is what I am testing:
And here is what I am seeing:
@@ -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)). |
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 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?
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)). |
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.
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.
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.
Better wording very welcome but here is my try.
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)). |
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.
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)
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.
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
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 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.
Closing in favor of vuejs/vue#9912. Thanks guys! |
As far as I can tell the PR that replaced this one ( vuejs/vue#9912 ) did not actually update any documentation that explains how to use Vue in a global context, which is what this PR was originally intended to do. Therefore I'd advocate reopening this PR. I'm actively having trouble trying to typecheck a JS file with TypeScript that uses Vue through the |
No description provided.