-
-
Notifications
You must be signed in to change notification settings - Fork 8.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
Support imported types in SFC macros #8083
Conversation
Will this work for global defineProps<UserData>() Would be super cool but my understanding is that the improved support is only for specifically imported types? |
Global types won't work - this can only work with explicitly imported types. |
Could there be a way to feed a list of The use case is for back-end integrations. We generate types from non-TypeScript code (PHP in this example) into defineProps<{
user: App.Data.UserData
}>() Instead of |
@innocenzi yes, that would be technically possible. Although I'll probably do that in a separate PR. |
That would be amazing. Thanks for this PR as well, super useful. |
Looking forward to it, when will it be available |
@innocenzi global types supported via 4e028b9 |
@yyx990803 Thank you so much! ❤️ |
Damn, amazing work! Thanks a ton! |
Time to update the doc. |
After using the 3.3.0 alpha version, an error is reported:
code like : interface MyComponentProps extends ComponentProps {
someprop: string
}
const props = withDefaults(defineProps<MyComponentProps>(), {
someprop: 'xxxx'
}) The above code works normally in version 3.2.x, |
@Hccake Would you mind providing a runnable minimal reproduction? |
@edison1105 Ok, the link is here: https://codesandbox.io/p/github/Hccake/vue3-typescript/master If you use vue 3.2.x version code can run normally |
import type {ButtonProps} from "ant-design-vue";
// ButtonProps = Partial<ExtractPropTypes<ReturnType<typeof buttonProps>>>;
@yyx990803 The error caused the code to not run as well as 3.2.x. |
<script setup lang="ts">
import inputProps from "ant-design-vue/es/input/inputProps";
defineProps(inputProps());
</script> do like this without wait for 3.3 |
The problem is not how to implement it, but that this change will cause existing code to be unable to upgrade smoothly |
@yyx990803 should we expect this feature in |
using imported types with defineProps is currently unsupported but has been fixed awaiting a release in vuejs/core#8083
using imported types with defineProps is currently unsupported but has been fixed awaiting a release in vuejs/core#8083
using imported types with defineProps is currently unsupported but has been fixed awaiting a release in vuejs/core#8083
using imported types with defineProps is currently unsupported but has been fixed awaiting a release in vuejs/core#8083
This would be very nice, although I couldn't think it will be. |
You can try betterDefine of Vue Macros. |
using imported types with defineProps is currently unsupported but has been fixed awaiting a release in vuejs/core#8083
using imported types with defineProps is currently unsupported but has been fixed awaiting a release in vuejs/core#8083
greetings! would really appreciate a confirmation: when we use something like while using relative path if this is the case, is there expectation for support of aliased paths? thanks so much! |
using imported types with defineProps is currently unsupported but has been fixed awaiting a release in vuejs/core#8083
Does it only support types? Or does it also support interfaces? |
using imported types with defineProps is currently unsupported but has been fixed awaiting a release in vuejs/core#8083
I get this error (only during build) when importing a type with relative path eg:
when I use project root import, it resolves without errors
I should note that this error arises when I import a file to use in defineProps() since we can do that now which is awesome 🙂 I fixed it with alias import, but I think it should be fixed for relative imports too |
Summary
SFC macros that need to generate runtime options from types (i.e.
defineProps
anddefineEmits
) can now directly reference imported types.Details
Relative Imports
The following will throw an error in <3.3, but will work after this PR:
In addition, more complex types are also supported, e.g. you can do:
Do note that complex types support is AST-based (not using TS itself) and therefore not 100% comprehensive. For example, you cannot use conditional types for the props type itself:
Module / Path Imports
When importing types from a non-relative source, e.g.:
Vue will use TypeScript's API to:
tsconfig.json
usingts.findConfigFile
compilerOptions
usingts.parseJsonConfigFileContent
(this step is cached)ts.resolveModuleName
This ensures the resolution is the same as user's configured TS environment, respecting the resolution and paths specified in
tsconfig.json
.Failed Resolves
Performance Implications
This feature can potentially cause
compiler-sfc
to resolve, parse, crawl, and analyze multiple additional type files. To avoid performance overhead, the analyzed result of the same file referenced by multiple components will be cached (and invalidated by bundler plugins), and the type reference crawl is performed lazily only when necessary. The overall performance overhead should be negligible in theory, but we will need to gather some feedback from real world projects.HMR
For this feature to work properly with HMR, it will require additional changes in
@vitejs/plugin-vue
andvue-loader
to support HMR on changes from imported types.@vitejs/plugin-vue
support landed in vitejs/vite-plugin-vue@c891652 (requires ^4.2.0-beta)vue-loader
support landed in vuejs/vue-loader@bbd98fc (requires ^17.1.0-beta)