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

Better typings for KeyboardEvent.key #143

Open
iway1 opened this issue May 2, 2023 · 3 comments
Open

Better typings for KeyboardEvent.key #143

iway1 opened this issue May 2, 2023 · 3 comments

Comments

@iway1
Copy link

iway1 commented May 2, 2023

KeyboardEvent.key is typed as a string, even though it's a union of string literals.

window.addEventListener('keydown', (e)=>{
  // string type
  e.key
})

Would be really nice to get autocomplete here =D

@qb20nh
Copy link

qb20nh commented Aug 1, 2023

https://www.w3.org/TR/uievents-key/#keys-unicode

Almost every Unicode character can be used as a valid key attribute value, but there is a small set of Unicode characters which MUST NOT be used. We introduce the concept of a key string to identify the set of Unicode strings that are appropriate for use as a key attribute value.

A key string is a string containing a 0 or 1 non-control characters ("base" characters) followed by 0 or more combining characters. The string MUST be in Normalized Form C (NFC) as described in [UAX15].

A non-control character is any valid Unicode character except those that are part of the "Other, Control" ("Cc") General Category.

A combining character is any valid Unicode character in the "Mark, Spacing Combining" ("Mc") General Category or with a non-zero Combining Class.

Listing all possible value is possible but impractical considering all the keyboard configurations around the globe and ever changing Unicode standard. Quick good enough solution could be only including US keyboard keys plus predefined keys.

@tommy-mitchell
Copy link

Related: microsoft/TypeScript#38886

@qb20nh
Copy link

qb20nh commented Jan 23, 2024

Hack to make autocompletion work: microsoft/TypeScript#29729 (comment)

type LiteralUnion<T extends U, U = string> = T | (U & { zz_IGNORE_ME?: never })

type Color = LiteralUnion<'red' | 'black'>

var c: Color = 'red'                    // Has intellisense
var d: Color = 'any-string'             // Any string is OK
var d: Color = { zz_IGNORE_ME: '' }     // { zz_IGNORE_ME } placeholder is at the bottom of intellisense list and errors because of never 

type N = LiteralUnion<1 | 2, number> // Works with numbers too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants