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

chore: Safe typing for attribute editor #1517

Merged
merged 6 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@
}
},
{
"files": ["none"],
"files": [
"src/attribute-editor/**"
],
"excludedFiles": [
"src/**/__tests__/**",
"src/**/__integ__/**",
Expand Down
5 changes: 4 additions & 1 deletion src/attribute-editor/row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ function render<T>(
itemIndex: number,
slot: AttributeEditorProps.FieldRenderable<T> | React.ReactNode | undefined
) {
return typeof slot === 'function' ? slot(item, itemIndex) : slot;
if (typeof slot === 'function') {
return (slot as AttributeEditorProps.FieldRenderable<T>)(item, itemIndex);
Copy link
Member

Choose a reason for hiding this comment

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

What is happening here? Isn't typescript by itself can figure that this is a function? (from typeof slot === 'function')

Copy link
Member

Choose a reason for hiding this comment

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

From the PR description

This PR removes unsafe usages of the any type

But I do not see any removed any's in this diff...

Copy link
Member Author

Choose a reason for hiding this comment

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

From typeof slot === 'function', Typescript infers the type as (item: T, itemIndex: number) => any. I could not find out precisely why it ends up with that type, but it is caused by the presence of React.ReactNode in line 35.

While the any type of the returned value of this function is not directly visible in the code, the rule still detects its implicit presence and prevents us from returning an any-typed value. With this new code, the return type of the render function is now React.ReactNode.

Copy link
Member

@just-boris just-boris Sep 6, 2023

Choose a reason for hiding this comment

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

In this case I would rather cast the return value than the function:

return slot(item, itemIndex) as React.ReactNode

Copy link
Member Author

@connorlanigan connorlanigan Sep 6, 2023

Choose a reason for hiding this comment

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

If we assert the type of the return value, we have to keep that type assertion in sync with the definition of the return type of FieldRenderable, which sounds more risky to me than keeping only FieldRenderable in sync only across line 35 and line 38

Copy link
Member

Choose a reason for hiding this comment

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

This is unsafe either way. We are not fixing any errors, just making the linter happy (which is another big discussion if these rules actually useful).

I prefer the shorter option because it is more readable, cannot see any runtime hazards, really.


When looking into this PR I also learned where this strange any is coming from. ReactNode types are incorrect in our version. Related PR DefinitelyTyped/DefinitelyTyped#56210, "Remove {} from ReactFragment" is the change we need

Copy link
Member

@just-boris just-boris Sep 6, 2023

Choose a reason for hiding this comment

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

Alternatively, we can write a separate typeguard function

function isSlotFunction(slot: unknown) slot is AttributeEditorProps.FieldRenderable<T> {
   return typeof slot == 'function'
}

...

if (isSlotFunction(slot)) {
  return slot(item, itemIndex);
}
return slot;

This is also safe and readable to my standards

}
return slot;
}

const GRID_DEFINITION = [{ colspan: { default: 12, xs: 9 } }];
Expand Down
Loading