Title: Suggestion: introduce UniveralElement to eliminate most type assertions in DOM programming (prototype created) · Issue #3304 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: introduce UniveralElement to eliminate most type assertions in DOM programming (prototype created) · Issue #3304 · microsoft/TypeScript
X Title: Suggestion: introduce UniveralElement to eliminate most type assertions in DOM programming (prototype created) · Issue #3304 · microsoft/TypeScript
Description: TS heavily relies on type assertion in DOM programming. In my experiences, type assertion is a major factor of produtivity-reduction in font-end TS. There is another user expressed similar concern (#3263). So I try to eliminate most type...
Open Graph Description: TS heavily relies on type assertion in DOM programming. In my experiences, type assertion is a major factor of produtivity-reduction in font-end TS. There is another user expressed similar concern ...
X Description: TS heavily relies on type assertion in DOM programming. In my experiences, type assertion is a major factor of produtivity-reduction in font-end TS. There is another user expressed similar concern ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/3304
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: introduce UniveralElement to eliminate most type assertions in DOM programming (prototype created)","articleBody":"TS heavily relies on type assertion in DOM programming. In my experiences, type assertion is a major factor of produtivity-reduction in font-end TS. There is another user expressed similar concern (#3263).\n\nSo I try to eliminate most type assertions in DOM programming by adding a `UniveralElement` interface to `lib.d.ts`, see [my fork of `lib.es6.d.ts`](https://github.com/duanyao/typescript-fluent-dom).\n\nIn my fork, `querySelector()`, `getElementById()`, `getElementsByClassName()`, `children`, `firstElementChild` etc. return `UniversalElement` or a list of `UniversalElement`, which inherits all known sub interfaces of `Element`; `UIEvent::target` is typed as `UniversalUIEventTarget`, which extends `UniversalElement`, `Document`, and `Window`. So in most case so you don't have to cast the result before use anymore. Additionally you don't loose the power of type check and autocompletion.\n\nA snapshot of autocompletion using my prototype:\n\n\u003cimg src=\"https://github.com/duanyao/typescript-fluent-dom/raw/master/ts-fluent-snapshot.png\" />\n\nAnd some sample codes:\n\n``` javascript\n/// \u003creference path=\"lib.es6.d.ts\" />\n\nvar ue = document.querySelector('#foo'); // UniversalElement\n// from Element\nvar cls: string = ue.className;\n// from HTMLElement\nue.blur();\n// from HTMLMediaElement\nue.play();\n\n// from HTMLInputElement\nvar files = document.querySelectorAll('input[type=file]')[0].files;\nvar fname = files[0].name;\n\n// from HTMLImageElement\nvar nh = document.body.children[0].naturalHeight;\n\nvar svg = document.getElementsByClassName(\"bar\")[0];\n// from SVGElement\nvar vp = svg.viewportElement;\n// from SVGLineElement\nsvg.x1.baseVal.newValueSpecifiedUnits(1, SVGLength.SVG_LENGTHTYPE_PX);\n\n// type of href from HTMLAnchorElement becomes \"any\" because of type confliction\n// with SVGElements\nvar href = document.querySelector('a').href;\n\n// UIEvent::target is UniversalUIEventTarget, which extends UniversalElement,\n// Document, and Window\ndocument.addEventListener('mousemove', ev => {\n var targ = ev.target;\n if (targ.classList.contains('movable')) {\n targ.style.left = ev.pageX + 'px';\n targ.style.right = ev.pageX + 'px';\n }\n});\n```\n\n@NekR and @kitsonk, what do you think?\n","author":{"url":"https://github.com/duanyao","@type":"Person","name":"duanyao"},"datePublished":"2015-05-29T18:13:10Z","url":"https://github.com/microsoft/TypeScript/issues/3304"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:9d3dd9d5-e167-d696-e206-3ab5e8af32c8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BF4C:2AB944:23C881:2FE8B1:6AFBE254 |
| html-safe-nonce | e9ef9c6267cbe7a31fa4723c2d6345916569d5676c4f778cda02491ef65f0834 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRjRDOjJBQjk0NDoyM0M4ODE6MkZFOEIxOjZBRkJFMjU0IiwidmlzaXRvcl9pZCI6Ijg2OTkxNDE1NTQ3MDQyMTI1NjQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b4ea36f1ad01196cf36f2ba0894e0354757e60c6a66a299abcfbdd1bc14e0158 |
| hovercard-subject-tag | issue:82529896 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/3304/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ce40a920e8af793c59f48b33d32b0413cdf158313854c1f540ea25486cde8971/microsoft/TypeScript/issues/3304 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ce40a920e8af793c59f48b33d32b0413cdf158313854c1f540ea25486cde8971/microsoft/TypeScript/issues/3304 |
| og:image:alt | TS heavily relies on type assertion in DOM programming. In my experiences, type assertion is a major factor of produtivity-reduction in font-end TS. There is another user expressed similar concern ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | duanyao |
| hostname | github.com |
| expected-hostname | github.com |
| None | 744d4381b1c4dbd6d8736c7d7e5bbe7dc11217584e457a3555020c631764e8ec |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | df859c48a4c8b4c0009c19bb4e82101aacb4b998 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width