-
-
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
feat: runtime-support for type unions in props #12059
base: main
Are you sure you want to change the base?
Conversation
* fix(runtime-core): withDefaults & union types * feat: union props * chore: add distributive union test * chore: improve tests
@vue/compiler-core
@vue/compiler-dom
@vue/compiler-sfc
@vue/compiler-ssr
@vue/reactivity
@vue/runtime-core
@vue/runtime-dom
@vue/server-renderer
@vue/shared
@vue/compat
vue
commit: |
Size ReportBundles
Usages
|
I would like to cherry-pick both this and #12108 but I get conflicts in d09f791#diff-ddb3174016350dd4464764b4a1ba232d09ad1e75164d4a37758c759d019202e4 How would you solve it in the context of the other PR? |
@edison1105 my PR contains an improvement (L331) to inferring props in combination with Edit: The test in #12108 passes when using my branch. Apparently, we've both fixed #12106, just in a different way. @edison1105 let me know which implementation you prefer. @darkbasic cherry-picking only this PR will be enough for the moment (at least according to the type tests). |
Thanks. I would like to test this in my codebase, could you please share how am I supposed to pack it?
Unfortunately I get some errors when trying to import some types: |
For every commit/PR in this repo, a package is released on pkg.pr.new. You can use the published package like this: In this case, 12059 points to the current PR. |
But this PR doesn't include #12077 which according to the description is needed. Could you please rebase against latest main so that a new package gets released? Thanks |
Hey, thanks for your input. With your example provided I don't see a solution to build a proper union. Your example boils down to the following: export interface NumberCard {
hasChart: boolean
chartOption: ChartOption
type: 'number'
value: number
}
export interface FractionCard {
hasChart: boolean
chartOption: ChartOption
type: 'fraction'
value: Fraction
}
export interface NoValueCard {
hasChart: true
chartOption: 'bar' | 'line'
}
export type Card = NumberCard | FractionCard | NoValueCard In your example, it's impossible to tell if the props are missing or not because |
It looks like this needs to be rebased now that #12108 got merged. |
One of the last missing features regarding TypeScript support in Vue is the ability to use discriminated- and distributive union types in props with runtime validation. This PR aims to address these limitations and provides a more flexible way to define props.
Fixes
Implementation
Through compiling and validating the new internal prop option
union
, we can skip validation for props that are not part of an active sub-union.In the following example,
a
andb
are in an active sub-union,c
andd
are not. Defining eithera
orb
should trigger warnings for missing props for the other prop in the same sub-union.Limitations
PublicProps
withProps
#12077 for union type-safety.Supported patterns
This proposal currently supports the following patters:
Discriminated union example
Distributive union example
Mentions
Shoutout to @KazariEX for helping me navigating various intricacies of the prop type system.