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

Enable type declaration guards #3123

Merged
merged 3 commits into from
Feb 10, 2023
Merged

Enable type declaration guards #3123

merged 3 commits into from
Feb 10, 2023

Conversation

colinrotherham
Copy link
Contributor

@colinrotherham colinrotherham commented Dec 19, 2022

Add instanceof checks where necessary to unblock type declaration checks in:

@colinrotherham colinrotherham linked an issue Dec 19, 2022 that may be closed by this pull request
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3123 December 19, 2022 21:39 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3123 January 16, 2023 16:08 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3123 January 18, 2023 15:54 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3123 January 18, 2023 16:46 Inactive
@romaricpascal romaricpascal linked an issue Jan 31, 2023 that may be closed by this pull request
11 tasks
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3123 February 2, 2023 12:10 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3123 February 3, 2023 08:14 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3123 February 6, 2023 17:20 Inactive
Base automatically changed from fix-types to main February 6, 2023 17:25
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3123 February 6, 2023 17:45 Inactive
return this
}

var $textarea = $module.querySelector('.govuk-js-character-count')
if (!$textarea) {
if (!($textarea instanceof HTMLTextAreaElement)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to allow text inputs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say yes, just as a safety to not break things if someone mounted the component on a markup with an <input> instead of <textarea>. I don't think we officially support it (given our template uses a textarea), but it does work.

I'm saying yes with the assumption that it's a matter of turning that if into if (!($textarea instanceof HTMLTextAreaElement || $textarea instanceof HTMLInputElement)), though. If it's more complex than that, I think we can stick to <textarea> and treat it in its own block of work for properly supporting input in the CharacterCount (both at JS and template level).

Kapture.2023-02-02.at.15.22.00.mp4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is where it gets tricky

File upload, checkbox and radio inputs are also HTMLInputElement

But in terms of guarding the .value property your example works great 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the following places we've gone further than HTMLElement:

Character count needs <textarea> or <input>
lint-types ./src/govuk/components/character-count/character-count.mjs#L65

Checkboxes need <input> with type="checkbox"
lint-types ./src/govuk/components/checkboxes/checkboxes.mjs#L179

Radios need <input> with type="radio"
lint-types ./src/govuk/components/radios/radios.mjs#L129

Details needs <details> to avoid polyfill
lint-types ./src/govuk/components/details/details.mjs#L40

Error summary needs <a> for links and <input> (checkbox/radio) for legend .scrollIntoView()
lint-types ./src/govuk/components/error-summary/error-summary.mjs#L114
lint-types ./src/govuk/components/error-summary/error-summary.mjs#L185

Skip link needs <a>
lint-types ./src/govuk/components/skip-link/skip-link.mjs#L14

Tabs need <a>
lint-types ./src/govuk/components/tabs/tabs.mjs#L270

@colinrotherham colinrotherham marked this pull request as ready for review February 6, 2023 20:15
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3123 February 8, 2023 11:08 Inactive
Copy link
Member

@romaricpascal romaricpascal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say we're good to go on that one, as I don't think any of these changes would be breaking. @36degrees I would appreciate a second look, though, in case we missed a potential breaking case when going through the changes with Colin.

colinrotherham and others added 3 commits February 10, 2023 09:25
Review component JSDoc types against module and element checks

We now prefer `{Element}` type to `{HTMLElement}` to maintain compatibility with `querySelector` but we continue to use `instanceof` to guard types

Co-Authored-By: Romaric <romaric.pascal@digital.cabinet-office.gov.uk>
Where empty objects are initially assigned (or merged) the compiler needs to know the final “shape” up front

In editors and IDEs this also helps populate tooltip messages during loops etc
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3123 February 10, 2023 09:25 Inactive
@colinrotherham colinrotherham merged commit 916e3d9 into main Feb 10, 2023
@colinrotherham colinrotherham deleted the fix-types-instanceof branch February 10, 2023 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Annotate our code with types and lint to ensure their proper usage
5 participants