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

Remove the index signature from TestContext #1301

Merged

Conversation

dfreeman
Copy link
Contributor

The TestContext type has a [key: string]: unknown index signature inherited from BaseContext. This has led to some confusion, as it muddies the error message folks see when they try to access fields that were present in the DT types but not the native ones, like element.

It also doesn't mesh super well with how folks tend to add custom properties to the context type by explicitly declaring them and then setting the this type appropriately for their tests. Ultimately we want for that pattern to go away entirely with <template>, of course, but in the meantime it would be nice to avoid extra confusion.

The index signature on BaseContext doesn't actually appear to be required, and ultimately the way we're using the type is equivalent to just object. Accordingly, I'm making BaseContext an alias for object here and otherwise attempting to touch the code as little as possible. The bits that did have to change were largely places we were implicitly taking advantage of the index signature to skirt typechecking.

@dfreeman dfreeman force-pushed the remove-index-signature-on-context branch from 354b3fc to cba01be Compare December 20, 2022 22:01
Comment on lines +85 to +88
let maybeContext = context as Record<string, unknown>;
return (
typeof context['pauseTest'] === 'function' &&
typeof context['resumeTest'] === 'function'
typeof maybeContext['pauseTest'] === 'function' &&
typeof maybeContext['resumeTest'] === 'function'
Copy link
Contributor

Choose a reason for hiding this comment

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

For a possible follow-on (keeping type changes separate from behavioral changes):

return (
  'pauseTest' in context && 
  typeof maybeContext['pauseTest'] === 'function' &&
  'resumTest' in context && 
  typeof maybeContext['resumeTest'] === 'function'
);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had that and my editor was happy with it, but it turns out the in narrowing for object landed in TS 4.9, and definitely didn't want to touch that as part of this 😅

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.

None yet

2 participants