-
Notifications
You must be signed in to change notification settings - Fork 441
Add more precise typing for autocomplete HTML attribute #1467
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
Add more precise typing for autocomplete HTML attribute #1467
Conversation
Thanks for the PR! This section of the codebase is owned by @saschanaz - if they write a comment saying "LGTM" then it will be merged. |
@microsoft-github-policy-service agree |
@microsoft-github-policy-service agree |
First, please define an enum (which then generates a union type) and use that. Second, are we sure every item here is well supported? Poorly supported items shouldn't be added. |
Typescript team should be confident that they want |
/cc @sandersn @weswigham |
@saschanaz: thanks, fixed (I didn't know how to do it with these json files, at first). I've re-read the spec and declared an I've also fixed the possible values for
I added Unfortunately, I don't know how to add typescript auto-completion for a list of strings ( Also note that this syntax/workaround is already used and tested in the TypeScript repo, |
What does It's also currently broken, please use |
With
|
@saschanaz: Sorry I don't understand that last part, the tests pass and it seems to work. I can't use |
Look at the output: |
Sorry I got it, the rest of the union is ok but
|
Do not add If the set of values is truly unconstrained, |
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.
Per prior comment
Yup, that's also what other properties do. |
It's not unconstrained. Writing I could try writing it as a string template literal type instead, based on this table from the spec, to allow 1, 2, 3 or 4 tokens from the union, in the correct order, but it would take me some time to do it right so we may want to first discuss if this could be investigated/accepted, or if it wouldn't be accepted either. |
just to clarify that this is to work around a bug in typescript; I would be happy to use See issues linked in #issuecomment-1377304118 for explanations. |
Here are the full constraints, in the right order, for the set of space-separated tokens: - `section-${string}`
- "shipping" | "billing"
- AutoFill // (the union I added to this PR, excluding "webauthn")
- "webauthn" |
@saschanaz, @RyanCavanaugh: Ok, here is how it would look like, it should be restricted to all authorized permutations (because for type AutoFillSection = `section-${string}`;
type AutoFillAddressKind = 'shipping' | 'billing';
type AutoFillNormalField =
| 'name'
| 'honorific-prefix'
| 'given-name'
| 'additional-name'
| 'family-name'
| 'honorific-suffix'
| 'nickname'
| 'username'
| 'new-password'
| 'current-password'
| 'one-time-code'
| 'organization-title'
| 'organization'
| 'street-address'
| 'address-line1'
| 'address-line2'
| 'address-line3'
| 'address-level4'
| 'address-level3'
| 'address-level2'
| 'address-level1'
| 'country'
| 'country-name'
| 'postal-code'
| 'cc-name'
| 'cc-given-name'
| 'cc-additional-name'
| 'cc-family-name'
| 'cc-number'
| 'cc-exp'
| 'cc-exp-month'
| 'cc-exp-year'
| 'cc-csc'
| 'cc-type'
| 'transaction-currency'
| 'transaction-amount'
| 'language'
| 'bday'
| 'bday-day'
| 'bday-month'
| 'bday-year'
| 'sex'
| 'url'
| 'photo';
type AutoFillContactKind = 'home' | 'work' | 'mobile' | 'fax' | 'pager';
type AutoFillContactField =
| 'tel'
| 'tel-country-code'
| 'tel-national'
| 'tel-area-code'
| 'tel-local'
| 'tel-local-prefix'
| 'tel-local-suffix'
| 'tel-extension'
| 'email'
| 'impp';
type AutoFillField =
| AutoFillNormalField
| AutoFillContactField
| `${AutoFillContactKind} ${AutoFillContactField}`;
type AutoFillCredentialField = 'webauthn';
type AutoFill =
| 'off'
| 'on'
| AutoFillField
| `${AutoFillSection} ${AutoFillField}`
| `${AutoFillAddressKind} ${AutoFillField}`
| `${AutoFillSection} ${AutoFillAddressKind} ${AutoFillField}`
| `${AutoFillSection} ${AutoFillAddressKind} ${AutoFillField} ${AutoFillCredentialField}`
| `${AutoFillAddressKind} ${AutoFillField} ${AutoFillCredentialField}`
| `${AutoFillSection} ${AutoFillField} ${AutoFillCredentialField}`
| `${AutoFillField} ${AutoFillCredentialField}`; Here are example suggestions and valid strings:
|
Internal tests are different from code which is shipped to many developers and shapes the way they work. |
@HolgerJeromin: Right. Does the above proposition sound better than the first one? With template string types? |
2f40faa
to
ca386d5
Compare
Hi @RyanCavanaugh, @saschanaz: I pushed a new version without See #1467 (comment) for screenshots and context. |
I turned the types into values and got 424: var OptionalPostfixToken = _ => 2
var OptionalPrefixToken = _ => 2
var AutoFillSection = 1
var AutoFillAddressKind = 2
var AutoFillBase = 2
var AutoFillContactField = 9
var AutoFillContactKind = 3
var AutoFillCredentialField = 1
var AutoFillNormalField = 36
var AutoFillField = AutoFillNormalField + OptionalPrefixToken(AutoFillContactKind) * AutoFillContactField;
var AutoFill = AutoFillBase + OptionalPrefixToken(AutoFillSection)*OptionalPrefixToken(AutoFillAddressKind)*AutoFillField*OptionalPostfixToken(AutoFillCredentialField)
console.log(AutoFill) I think that's reasonable; any sizeable addition to the DOM adds that many types. |
Replaces: microsoft/TypeScript#52169 This will allow to autocomplete… the `autoComplete` attribute in vscode for example.  The list is quite long and it's not easy remembering the spelling of each one of them (first_name or given-name? password or current-password?), and it would improve the UX of sites if they were more appropriately used (for example, some people may think that the input's `type` and/or `name` attributes may be enough for fields like email or password, but knowing to use `autoComplete="new-password"` or `one-time-code` can be really useful).
Add missing value according to spec (`webauthn`).
Don't list all possible alterations manually. microsoft#1467 (comment)
d3cb416
to
b3466ed
Compare
@saschanaz So looks like everything is good on that page, right? The only tests that fail on two browsers are with I've rebased, is it ready to merge? :) |
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.
Sorry for the long delay, and thank you for maintaining this so far!
Looks good with some nits. It seems a bit more fields are supported now than this was written, but asking that change here would be very unfair, that can be done separately.
}, | ||
{ | ||
"name": "AutoFillSection", | ||
"overrideType": "`section-${string}`" |
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.
Can you add a TODO comment about the over-match of section-{string}
?
I'm assuming that having the Optional*Token helper types here is okay, as @sandersn hasn't complained about that. |
@saschanaz: pushed, thanks! :) |
Let's see I can merge this even if there's a change request, because that's solved now. LGTM |
Merging because @saschanaz is a code-owner of all the changes - thanks! |
@sandersn, @saschanaz: Hi, do you when it will be included in https://github.com/microsoft/TypeScript/blob/main/src/lib/dom.generated.d.ts and released? 😄 |
(I'm not really allowed to know such info, but the roadmap implies the new release is coming soonish, as the release happens roughly every three months and the last release was three months ago.) https://github.com/microsoft/TypeScript/wiki/Roadmap) |
If you need it today, consider using https://www.npmjs.com/package/@types/web. ... except it looks like the release step is broken since 0.0.99. (Edit: Filed #1580) |
I'll check out the publication failure and try to fix it. Edit: Oh, looks you have a potential fix already. I'll merge that. |
Unfortunately, this does not get picked up by Asking for instructions here: DefinitelyTyped/DefinitelyTyped#66982 |
This will allow to autocomplete… the
autoComplete
attribute in vscode for example.The list is quite long and it's not easy remembering the spelling of each one of them (first_name or given-name? password or current-password?), and it would improve the UX of sites if they were more appropriately used (for example, some people may think that the input's
type
and/orname
attributes may be enough for fields like email or password, but knowing to useautoComplete="new-password"
orone-time-code
can be really useful).I tried declaring atype AutoComplete
to avoid repetition but didn't find how to do that:microsoft/TypeScript#52169.
See updated proposal at #1467 (comment)
Fixes microsoft/TypeScript#52168.
See also #71.