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

Conversation

realmerx
Copy link

No description provided.

Copy link
Member

@phanan phanan left a 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.

src/v2/guide/typescript.md Outdated Show resolved Hide resolved
@phanan phanan changed the title Update typescript.md to describe how to use typescript types without module bundler Update typescript.md to describe how to use typescript types without module bundler (closes #2042) Apr 22, 2019
…out module bundler


grammar fixes by phanan

Co-Authored-By: realmerx <realmerx@gmail.com>
@@ -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
Copy link
Member

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.

Copy link
Author

@realmerx realmerx Apr 22, 2019

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).

Copy link
Member

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.

Copy link
Author

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:

https://github.com/vuejs/vuejs.org/blob/6e6891eeb139c6dc712169359677e1bf72dd3c4b/src/v2/guide/typescript.md

And here is what I am seeing:

image

@@ -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.

@phanan
Copy link
Member

phanan commented Apr 24, 2019

Closing in favor of vuejs/vue#9912. Thanks guys!

@phanan phanan closed this Apr 24, 2019
@davidfstr
Copy link

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 window.Vue global, as covered in my forum post here, so having updated documentation would be quite useful!

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.

4 participants