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

Experimental RFC 436 - cannot use generic prop as object key in template without props #2192

Closed
cohlar opened this issue Dec 11, 2022 · 3 comments
Labels
good reproduction ✨ This issue provides a good reproduction, we will be able to investigate it first upstream

Comments

@cohlar
Copy link

cohlar commented Dec 11, 2022

With defineProps, the props are normally available in the template without the need to use const props = defineProps<...>() and adding props. before the prop name.
However when adding a generic prop as a key to an object in the HTML template, we get an error.

image

I made a public repo to showcase the issue. The generic component with the error is TheWelcome - please note this repo is shared with #2191 (separate branches).

@johnsoncodehk
Copy link
Member

This is the DefineComponent type behavior, T is recognized as PropType to resolve.

@pikax I'm pinging you here, but maybe I can try to fix it when I am free.

@cohlar
Copy link
Author

cohlar commented Feb 22, 2023

Hi, when I try upgrading vue from 3.2.45 to 3.2.47, I suddenly get a lot of type errors (from vue-tsc 1.1.7) on the props of components that use generics. I'm not 100% sure but I have a feeling it is related to this issue.

@johnsoncodehk johnsoncodehk added the good reproduction ✨ This issue provides a good reproduction, we will be able to investigate it first label Apr 22, 2023
@johnsoncodehk
Copy link
Member

@pikax naively changing these few lines would avoid the problem, but I don't know how much this would break. I'll leave this issue for upstream attention.

@cohlar can you open an issue on the vuejs/core repo?

type InferPropType<T> = [T] extends [null]
  ? any // null & true would fail to infer
  : [T] extends [{ type: null | true }]
  ? any // As TS issue https://github.com/Microsoft/TypeScript/issues/14829 // somehow `ObjectConstructor` when inferred from { (): T } becomes `any` // `BooleanConstructor` when inferred from PropConstructor(with PropMethod) becomes `Boolean`
  : [T] extends [ObjectConstructor | { type: ObjectConstructor }]
  ? Record<string, any>
  : [T] extends [BooleanConstructor | { type: BooleanConstructor }]
  ? boolean
  : [T] extends [DateConstructor | { type: DateConstructor }]
  ? Date
  : [T] extends [(infer U)[] | { type: (infer U)[] }]
  ? U extends DateConstructor
    ? Date | InferPropType<U>
    : InferPropType<U>
-   : [T] extends [Prop<infer V, infer D>]
-   ? unknown extends V
-     ? IfAny<V, V, D>
-     : V
+   : [T] extends [PropType<infer V>] ? V
+   : [T] extends [{ type: PropType<infer V> }] ? V
  : T

https://github.com/vuejs/core/blob/ae5a9323b7eaf3dfa11b2a442a02faa992f88225/packages/runtime-core/src/componentProps.ts#L125-L128

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good reproduction ✨ This issue provides a good reproduction, we will be able to investigate it first upstream
Projects
None yet
Development

No branches or pull requests

2 participants