-
Notifications
You must be signed in to change notification settings - Fork 794
fix(PinInput): remove default generic type
#4346
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
base: v3
Are you sure you want to change the base?
Conversation
commit: |
@@ -46,7 +46,7 @@ export type PinInputEmits<T extends PinInputType = 'text'> = PinInputRootEmits<T | |||
|
|||
</script> | |||
|
|||
<script setup lang="ts" generic="T extends PinInputType = 'text'"> | |||
<script setup lang="ts" generic="T extends PinInputType"> |
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.
Indeed, this change is necessary there should be no default here. However, the playground should work with just string[]
since the default type is text
π€
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.
That's what I was looking into but not there are no string[] | number[]
definition. The Reka UI component defines it like this: type PinInputValue<Type extends PinInputType = 'text'> = Type extends 'number' ? number[] : string[]
. This should work, I'm confused π
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've looked into this as well, and it does correctly infer the type of the complete event when type="text" is explicitly specified, like in <UPinInput type="text" @ complete="onComplete" />.
However, I'm also unsure why TypeScript fails to infer the correct type when relying on the default type value.
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.
@benjamincanac
Additionally, could you let me know the estimated release schedule for the version that includes reka-ui 2.3.1 (e.g., 3.1.4)?
Currently, it seems there's an issue where, in the current version, when the PinInput type is set to number, trying to clear the value results in it changing to 0 instead of being cleared.
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.
@KIM-DONGJU I'll make a release this week for sure!
Did you find a way to make this work? Not sure if we can merge this as is π¬
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βve been juggling a few things on my end, so I havenβt been able to give this the attention it deserves. Iβll look into it more thoroughly soon.
type
type
type
@Teages You seemed to understand TypeScript pretty well, we're stuck on this matter if you have some time to take a look it would help a lot π |
That is wired... The code should just work without this PR. Could you provide a minimal reproduction for your issue? @KIM-DONGJU <script setup lang="ts">
const strPin = ref<string[]>([])
const numPin = ref<number[]>([])
function expectStr(_pin: string[]) {}
function expectNum(_pin: number[]) {}
</script>
<template>
<UApp>
<div class="w-screen h-screen flex items-center justify-center flex-col gap-4">
<UPinInput v-model="strPin" @complete="expectStr" />
<UPinInput v-model="numPin" type="number" @complete="expectNum" />
<UPinInput v-model="strPin" type="text" @complete="expectStr" />
<!-- @vue-expect-error catch it -->
<UPinInput v-model="numPin" />
<!-- @vue-expect-error catch it -->
<UPinInput v-model="numPin" type="text" />
<!-- @vue-expect-error catch it -->
<UPinInput v-model="strPin" type="number" />
<!-- @vue-expect-error catch it -->
<UPinInput @complete="expectNum" />
<!-- @vue-expect-error catch it -->
<UPinInput type="text" @complete="expectNum" />
<!-- @vue-expect-error catch it -->
<UPinInput type="number" @complete="expectStr" />
</div>
</UApp>
</template> β nuxt-ui-playground git:(main) β pnpm why @nuxt/ui
Legend: production dependency, optional only, dev only
nuxt-app ~/nuxt-ui-playground (PRIVATE)
dependencies:
@nuxt/ui-pro 3.2.0
βββ @nuxt/ui 3.2.0
β nuxt-ui-playground git:(main) β pnpm list typescript
Legend: production dependency, optional only, dev only
nuxt-app ~/nuxt-ui-playground (PRIVATE)
devDependencies:
typescript 5.8.3 |
@Teages I do reproduce, try autocomplete the |
Oh, I misunderstood the problem, let me take another look |
Ok, I have an idea. @benjamincanac
// just a quick idea here, not the best way
type PinInputValue<Type extends PinInputType = 'text'> =
Type extends 'number'
? Type extends 'text'
? number[]
: string[] // it is undefined
: string[];
// or we can make a `Equal` type
// from https://github.com/type-challenges/type-challenges/blob/fbd74f4067fb43ebbf020f864ef404e99deb585f/utils/index.d.ts#L7
type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? true : false So what's happening?We can make a minimal reproduction for it: type PinInputType = 'text' | 'number'
type PinInputValue<Type extends PinInputType = 'text'> =
Type extends 'number'
? number[]
: string[]
export interface Props<T extends PinInputType> {
type?: T
value?: PinInputValue<T>
}
const builder = <T extends PinInputType = 'text'> (props: Props<T>): PinInputValue<T> => null as any
const a = builder({ type: })
// ^ typing here
type A = typeof a When we input after |
@@ -4,7 +4,7 @@ import theme from '#build/ui/pin-input' | |||
const sizes = Object.keys(theme.variants.size) as Array<keyof typeof theme.variants.size> | |||
const variants = Object.keys(theme.variants.variant) as Array<keyof typeof theme.variants.variant> | |||
|
|||
const onComplete = (e: string[]) => { | |||
const onComplete = (e: string[] | number[]) => { |
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 think we need to reverse this after unovue/reka-ui#2023 were merged. It can be type safe then.
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.
Done!
Thanks a lot for the PR on Reka UI π Not sure to understand how both of these are different π€ type PinInputValue<Type extends PinInputType = 'text'> =
Type extends 'number'
? Type extends 'text'
? number[]
: string[] // it is undefined
: string[]; type PinInputValue<Type extends PinInputType = 'text'> = Type extends 'number' ? number[] : string[]; |
Everyone know it: type A = ('number' | 'string') extends 'number' ? true : false
// ^ false, that is we want However, this it is different type Check<T> = T extends 'number' ? true : false
type B = Check<'number' | 'string'> // it is boolean, why??? In fact, TypeScript calculate it like this: type C = Check<'number'> | Check<'string'> // of course, it is boolean The better way to avoid this is using type BetterCheck<T> = [T] extends ['number'] ? true : false
type D = BetterCheck<'number' | 'string'> // it is false now |
π Linked issue
β Type of change
π Description
This PR updates the PinInput component to support 'number' as a valid value for the type prop.
Previously, the component only accepted 'text', which limited use cases where numeric input was desired (e.g., PIN codes, OTPs). While the documentation suggested that both 'text' and 'number' were supported, the actual type definition restricted the type prop to string onlyβresulting in a TypeScript error when attempting to use type="number".
This fix corrects the type definition to explicitly allow both 'text' and 'number', aligning the implementation with the documented behavior and ensuring type safety without affecting existing functionality.
Changes include:
defineProps
to explicitly support both'text'
and'number'
typesπ Checklist