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

useMagicKeys with the TS option "noUncheckedIndexedAccess" makes it impossible to use #3896

Closed
7 tasks done
Ericlm opened this issue Mar 30, 2024 · 1 comment
Closed
7 tasks done

Comments

@Ericlm
Copy link

Ericlm commented Mar 30, 2024

Describe the bug

When using useMagicKeys, the issue arises as soon as we activate the noUncheckedIndexedAccess inside of tsconfig.

This option allows Typescript to make all index-based accesses undefinable : https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess

The issue is that the return type of each of the magic key ref becomes ComputedRef<boolean> | undefined, and then make it impossible to use in any watch, watchEffect, or whenever for example.

Reproduction

https://github.com/Ericlm/vueuse-magic-issue

System Info

System:
    OS: macOS 14.4.1
    CPU: (8) arm64 Apple M1
    Memory: 190.52 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.8.0 - ~/.asdf/installs/nodejs/20.8.0/bin/node
    npm: 10.1.0 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Chrome: 120.0.6099.234
    Safari: 17.4.1
    Safari Technology Preview: 17.4
  npmPackages:
    @vueuse/core: ^10.9.0 => 10.9.0 
    vue: ^3.4.21 => 3.4.21

Used Package Manager

npm

Validations

@Ericlm Ericlm changed the title Using useMagicKeys with the TS option "noUncheckedIndexedAccess" makes it impossible to use useMagicKeys with the TS option "noUncheckedIndexedAccess" makes it impossible to use Apr 8, 2024
@Ericlm
Copy link
Author

Ericlm commented Apr 29, 2024

As the result of useMagicKeys() is a Record, that's why it can be undefined. However, it's possible to use something like :

const keys = useMagicKeys()

const debugKeys = computed<boolean>(() => {
  if (typeof keys['A+N+V'] === 'undefined') return false
  return keys['A+N+V'].value
})

With this trick, we ensure a safe access to the record, so it's not a problem from the VueUse side 😃

@Ericlm Ericlm closed this as completed Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant