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 TS type-checking on #core/assert #37171

Merged
merged 7 commits into from
Dec 10, 2021

Conversation

rcebulko
Copy link
Contributor

@rcebulko rcebulko commented Dec 9, 2021

TS supports type narrowing via type assertions, but does so in a way that is mutually exclusive with type-checking a returned value. See microsoft/TypeScript#34636 and microsoft/TypeScript#40562

Workaround: Type-checking for now will support assertion format, but code itself will still return the asserted values as normal (by casting to void). This means that as code is migrated to pass type-checking, it'll have to update how it calls these assertion functions, but any existing code relying on the return pattern will continue to function as normal.

Explanation of why this is annoying/doesn't fit both use-cases:

/** @return {x is Element} */
function isElement(x) { ... }

/** @return {asserts x is Element} */
function assertElement(x) { ... }

// The first mostly helps in conditionals like
if (isElement(e)) {
  e.getAttribute('foo'); // this is fine, because isElement "returns true iff e is Element"
}

assertElement(bar);
bar.getAttribute('foo'); // this is fine, because assertElement "asserts bar is Element"

// this can't work in TS, because any assertion return type is equivalent to void
assertElement(bar).getAttribute('foo');

@rcebulko rcebulko requested a review from samouri December 9, 2021 21:29
@amp-owners-bot
Copy link

amp-owners-bot bot commented Dec 9, 2021

Hey @jridgewell! These files were changed:

src/core/3p-frame-messaging.js
src/core/amp-config.extern.js
src/core/assert.shame.d.ts
src/core/assert/base.js
src/core/assert/dev.js
src/core/assert/error.d.ts
src/core/assert/index.js
src/core/assert/user.js
src/core/dom/index.js
src/core/dom/jsx.js
src/core/dom/layout/intersection-no-root.js
src/core/dom/layout/intersection.js
+9 more

@@ -79,19 +77,18 @@ export function devAssert(
* For more details see `assert`.
*
* @param {*} shouldBeElement
* @param {!Array<*>|string=} opt_message The assertion message
* @return {!Element} The value of shouldBeTrueish.
* @param {Array<*>|string=} opt_message The assertion message
Copy link
Member

Choose a reason for hiding this comment

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

Didn't we discuss preferring T[] vs. Array

Suggested change
* @param {Array<*>|string=} opt_message The assertion message
* @param {any[]|string=} opt_message The assertion message

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. I'm not positive if any works in JSDoc comments 2) Yes, I agree, but also I haven't been updating it everywhere. Would prefer to set up TSLint and let it do it for us

Copy link
Member

Choose a reason for hiding this comment

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

  1. any does work
  2. makes sense, especially if its auto-fixable.

src/core/dom/parse-date-attributes.js Show resolved Hide resolved
@@ -8,6 +8,8 @@
"strictNullChecks": true,
"lib": ["dom", "esnext", "es6"],
"paths": {
"#core/assert": ["./assert"],
Copy link
Member

Choose a reason for hiding this comment

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

nit: does this work:

'#core': ['.']
'#core/*': ['./*']

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will once context is allowed (yes we could exclude context directly instead). That's where we'll be very soon.

@rcebulko rcebulko enabled auto-merge (squash) December 10, 2021 19:45
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.

3 participants