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

useId return type is potentially undefined #11897

Closed
jack-allocate opened this issue Sep 11, 2024 · 5 comments
Closed

useId return type is potentially undefined #11897

jack-allocate opened this issue Sep 11, 2024 · 5 comments

Comments

@jack-allocate
Copy link

Vue version

3.5.4

Link to minimal reproduction

https://play.vuejs.org/#eNp9kEFLAzEQhf9KyKUKZT2ol9oKKj3Ug4p6DMiSna6p2SQkk3VhyX93kqW1gvSybN73MvPyRn7nXNVH4Au+DNIrhywARsd0bdqV4BgEvxVGdc56ZCOLATYNS2zrbcdmdHEmjDDSmoAMgT6ryXJ2fiPM8mIaSQPogNA5XSPQibFxzO6UsukI8DltpGlb1Va7YA3FGrNdcGk7pzT4Z4eKtgm+YIVkVmttvx+Lhj7CfK/LT5Bf/+i7MGRN8BcPAXwPgh8Y1r4FnPD67QkG+j/AzjZRk/sEfIVgdcwZJ9t9NA3FPvKVtJtSqDLte1gPCCbsH/UnaECvZAmT5aymwgSn5h9ONPL7isvqutwTJlG5Hz34vIp6JVBd8fQDoa+puA==

Steps to reproduce

Hover over id to see the type

What is expected?

Return type is string

What is actually happening?

Return type is string | undefined

System Info

No response

Any additional comments?

No response

@JohnieXu
Copy link

that's right, it should be string | undefined

export function useId(): string | undefined {
const i = getCurrentInstance()
if (i) {
return (i.appContext.config.idPrefix || 'v') + '-' + i.ids[0] + i.ids[1]++
} else if (__DEV__) {
warn(
`useId() is called when there is no active component ` +
`instance to be associated with.`,
)
}
}

@edison1105 edison1105 closed this as not planned Won't fix, can't repro, duplicate, stale Sep 12, 2024
@andrey-hohlov
Copy link

Does anyone else finds it annoying to always cast it to string?

const id = useId() as string;

@sgoess
Copy link

sgoess commented Sep 16, 2024

I have also encountered this issue today. It's new to me that the return type of useId can be undefined, as this is not documented in the Vue documentation: https://vuejs.org/api/composition-api-helpers#useid

There is also some discussion about this here: #11404

I'm not sure how to handle an undefined value in this context. It would be very helpful if the documentation could be updated to reflect this behavior, along with some guidance on how to manage the possibility of undefined.

Thanks!

@edison1105
Copy link
Member

const id = useId()!

@yyx990803
Copy link
Member

After thinking about this, we do already have other helpers like useAttrs() and useTemplateRef() that return a non-nullable type even when there is no active instance, and I think we should keep useId() consistent with that (i.e. always return a string). See a177092

@github-actions github-actions bot locked and limited conversation to collaborators Oct 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants