-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
report(tsc): infer createElement type from tag name #6637
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -18,6 +18,8 @@ | |||
|
||||
/* globals URL self */ | ||||
|
||||
/** @typedef {HTMLElementTagNameMap & {[id: string]: HTMLElement}} HTMLElmentByTagName */ | ||||
|
||||
class DOM { | ||||
/** | ||||
* @param {Document} document | ||||
|
@@ -27,14 +29,14 @@ class DOM { | |||
this._document = document; | ||||
} | ||||
|
||||
// TODO(bckenny): can pass along `createElement`'s inferred type | ||||
/** | ||||
* @param {string} name | ||||
* @template {string} T | ||||
* @param {T} name | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we type this as the keys of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We could, though it would eliminate the fallback to the generic There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would we still get helpful completion if it were strict options | string? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
sadly no, it's smart about it and coalesces to just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, I tried to use just
When Edge gets |
||||
* @param {string=} className | ||||
* @param {Object<string, (string|undefined)>=} attrs Attribute key/val pairs. | ||||
* Note: if an attribute key has an undefined value, this method does not | ||||
* set the attribute on the node. | ||||
* @return {Element} | ||||
* @return {HTMLElmentByTagName[T]} | ||||
*/ | ||||
createElement(name, className, attrs = {}) { | ||||
const element = this._document.createElement(name); | ||||
|
@@ -58,13 +60,14 @@ class DOM { | |||
} | ||||
|
||||
/** | ||||
* @template {string} T | ||||
* @param {Element} parentElem | ||||
* @param {string} elementName | ||||
* @param {T} elementName | ||||
* @param {string=} className | ||||
* @param {Object<string, (string|undefined)>=} attrs Attribute key/val pairs. | ||||
* Note: if an attribute key has an undefined value, this method does not | ||||
* set the attribute on the node. | ||||
* @return {Element} | ||||
* @return {HTMLElmentByTagName[T]} | ||||
*/ | ||||
createChildOf(parentElem, elementName, className, attrs) { | ||||
const element = this.createElement(elementName, className, attrs); | ||||
|
@@ -122,7 +125,7 @@ class DOM { | |||
|
||||
// Append link if there are any. | ||||
if (linkText && linkHref) { | ||||
const a = /** @type {HTMLAnchorElement} */ (this.createElement('a')); | ||||
const a = this.createElement('a'); | ||||
a.rel = 'noopener'; | ||||
a.target = '_blank'; | ||||
a.textContent = linkText; | ||||
|
@@ -147,7 +150,7 @@ class DOM { | |||
const [preambleText, codeText] = parts.splice(0, 2); | ||||
element.appendChild(this._document.createTextNode(preambleText)); | ||||
if (codeText) { | ||||
const pre = /** @type {HTMLPreElement} */ (this.createElement('code')); | ||||
const pre = this.createElement('code'); | ||||
pre.textContent = codeText; | ||||
element.appendChild(pre); | ||||
} | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -364,8 +364,7 @@ class ReportUIFeatures { | |
// load event, however it is cross-domain and won't fire. Instead, listen | ||
// for a message from the target app saying "I'm open". | ||
const json = reportJson; | ||
window.addEventListener('message', function msgHandler(/** @type {Event} */ e) { | ||
const messageEvent = /** @type {MessageEvent} */ (e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no idea what was going on here. Maybe fixed on the compiler/built-in defs side so it was not only inferred, but inferred as a |
||
window.addEventListener('message', function msgHandler(messageEvent) { | ||
if (messageEvent.origin !== VIEWER_ORIGIN) { | ||
return; | ||
} | ||
|
@@ -469,7 +468,7 @@ class ReportUIFeatures { | |
const ext = blob.type.match('json') ? '.json' : '.html'; | ||
const href = URL.createObjectURL(blob); | ||
|
||
const a = /** @type {HTMLAnchorElement} */ (this._dom.createElement('a')); | ||
const a = this._dom.createElement('a'); | ||
a.download = `${filename}${ext}`; | ||
a.href = href; | ||
this._document.body.appendChild(a); // Firefox requires anchor to be in the DOM. | ||
|
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.
HTMLElementTagNameMap
is the built-in you're talking about?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.
yes