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

react-autosuggest: restrict input props #39186

Merged
merged 1 commit into from
Oct 16, 2019
Merged

react-autosuggest: restrict input props #39186

merged 1 commit into from
Oct 16, 2019

Conversation

OliverJAsh
Copy link
Contributor

@OliverJAsh OliverJAsh commented Oct 16, 2019

This brings InputProps inline with React's types for input. That is, the following errors in React:

<input anything={false} />

This also means that keyof InputProps<T> returns a union of literals instead of string. This is helpful because it means that keys will be validated in operations such as Pick<InputProps<T>, 'autoFocus'>.


Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Add or edit tests to reflect the change. (Run with npm test.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

Select one of these and delete the others:

If changing an existing definition:

  • Provide a URL to documentation or source code which provides context for the suggested changes: <>
  • If this PR brings the type definitions up to date with a new version of the JS library, update the version number in the header.
  • If you are making substantial changes, consider adding a tslint.json containing { "extends": "dtslint/dt.json" }. If for reason the any rule need to be disabled, disable it for that line using // tslint:disable-next-line [ruleName] and not for whole package so that the need for disabling can be reviewed.

@OliverJAsh OliverJAsh marked this pull request as ready for review October 16, 2019 16:02
@typescript-bot
Copy link
Contributor

typescript-bot commented Oct 16, 2019

@OliverJAsh Thank you for submitting this PR!

🔔 @nicolas-schmitt @pjo256 @robessog @tbayne @cdeutsch @rosskevin @ThomasdenH @ulrichb - please review this PR in the next few days. Be sure to explicitly select Approve or Request Changes in the GitHub UI so I know what's going on.

If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead.

@typescript-bot typescript-bot added Owner Approved A listed owner of this package signed off on the pull request. Merge:Express and removed Awaiting reviewer feedback labels Oct 16, 2019
@typescript-bot
Copy link
Contributor

A definition owner has approved this PR ⭐️. A maintainer will merge this PR shortly. If it shouldn't be merged yet, please leave a comment saying so and we'll wait. Thank you for your contribution to DefinitelyTyped!

@typescript-bot
Copy link
Contributor

👋 Hi there! I’ve run some quick measurements against master and your PR. These metrics should help the humans reviewing this PR gauge whether it might negatively affect compile times or editor responsiveness for users who install these typings.

Let’s review the numbers, shall we?

Comparison details 📊
master #39186 diff
Batch compilation
Memory usage (MiB) 125.3 132.8 +5.9%
Type count 23994 23998 0%
Assignability cache size 53952 53953 0%
Language service
Samples taken 712 719 +1%
Identifiers in tests 712 719 +1%
getCompletionsAtPosition
    Mean duration (ms) 694.0 697.9 +0.6%
    Mean CV 7.2% 7.4%
    Worst duration (ms) 959.8 907.0 -5.5%
    Worst identifier relatedTarget theme
getQuickInfoAtPosition
    Mean duration (ms) 646.6 647.4 +0.1%
    Mean CV 8.2% 8.0% -2.5%
    Worst duration (ms) 844.3 865.2 +2.5%
    Worst identifier onBlur className

It looks like nothing changed too much. I won’t post performance data again unless it gets worse.

@typescript-bot typescript-bot added the Perf: Same typescript-bot determined that this PR will not significantly impact compilation performance. label Oct 16, 2019
@andrewbranch andrewbranch merged commit 854c503 into DefinitelyTyped:master Oct 16, 2019
@thenovelnomad
Copy link

This may be a case of your bug is my feature, but this broke our build process, and while I won't argue that it should not have been changed, it maybe should not have just been a patch version change.

@andrewbranch
Copy link
Member

Hm, sorry to hear that @thenovelnomad. Unfortunately, the way @types versions work is that the major and minor versions track the JS library, and then the patch number increments every time there’s a change to the typings that doesn’t reflect a new major/minor version of the JS library. When typings are updated to be “more accurate,” that often means stricter, which could mean breaks for consumers. The only way to avoid shipping those breaks on a patch version is to wait for the JS library to release a new major or minor and ship the typings improvement after bumping its version to match the new JS version. That probably should have been considered here, but it’s not always feasible to do. 😕

@thenovelnomad
Copy link

Ahh, thanks for the details, @andrewbranch. That makes sense. I guess we should lock down our types packages to the patch version.

@@ -68,7 +68,6 @@ declare namespace Autosuggest {
onChange(event: React.FormEvent<any>, params: ChangeEvent): void;
onBlur?(event: React.FocusEvent<any>, params?: BlurEvent<TSuggestion>): void;
value: string;
[key: string]: any;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is definitely not correct as everything you pass as inputProps into the component you get as param of renderInputComponent

Copy link
Contributor

@dolezel dolezel Oct 17, 2019

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@OliverJAsh OliverJAsh Oct 17, 2019

Choose a reason for hiding this comment

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

@dolezel Please can you provide a reduced test case demonstrating the error you're seeing, in a new issue?

Copy link
Contributor

Choose a reason for hiding this comment

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

I've posted link above. E.g. here https://github.com/mui-org/material-ui/blob/master/docs/src/pages/components/autocomplete/IntegrationAutosuggest.js#L199 it complains that 'classes' does not exist in type 'InputProps', but it should be possible to pass anything through and access it (e.g. https://github.com/mui-org/material-ui/blob/master/docs/src/pages/components/autocomplete/IntegrationAutosuggest.js#L50)

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 see. In that case, InputProps should probably be a generic that is shared with renderInputComponent. Until someone is able to make that change, I would suggest using a cast on the properties which are causing this problem (e.g. classes):

inputProps={{
  // We want to pass these extra props through to `renderInputComponent`. A cast is needed because they are not included in the `InputProps` type.
  ...{ classes } as InputProps<T>,
  id: 'whatever',
  // other properties
}}

Copy link
Contributor

@ulrichb ulrichb Nov 5, 2019

Choose a reason for hiding this comment

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

Ahhh renderInputComponent, I see.

In this case I would recommend to add the ref member not to InputProps but to the RenderInputComponent callback type (line 123).
Something like (inputProps: InputProps<TSuggestion> & { readonly ref: (instance: HTMLInputElement) => void }) => ....

This would avoid adding it to InputProps thus as input value for the AutoSuggest component (which would be misleading/wrong because it will get overwritten by AutoSuggest).

Copy link
Contributor

@cdeutsch cdeutsch Nov 18, 2019

Choose a reason for hiding this comment

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

Since renderInputComponent is a thing and you can render components from other libraries in it, I don't think [key: string]: any; should have been removed.

This broke the code I was using with Ant Design, which supports props like prefix and suffix

Choose a reason for hiding this comment

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

It also broke our implementation with data- prefixed properties.
It's valid in React so why should it be removed?
Maybe we should extend from a different interface to maintain backward compatibility as this change was "patch" but was breaking change for us.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

InputProps should probably be a generic that is shared with renderInputComponent

This is the correct fix that doesn't remove type safety. Would someone like to send a PR? I'm happy to review it.

Copy link

@lemieszek lemieszek Jun 1, 2020

Choose a reason for hiding this comment

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

dataset is valid thing in HTML. Yet, we still can't define regex on properties names so we can't type safety date-name properties.
If we could do that (there is a proposal: microsoft/TypeScript#6579), it would be so easy to implement:

// DOM data attributes later used in HTMLOrSVGElement.dataset
interface DataAttributes {
    [key: /data-\w+/]: number | string | boolean;
}

yet as for now, we could do:

// DOM data attributes later used in HTMLOrSVGElement.dataset
interface DataAttributes {
    [key: string]: string;
}

But it will break whole type safety on everything 🙈

@OliverJAsh OliverJAsh deleted the oja/react-autosuggest/restrict-input-props branch October 17, 2019 15:38
chivesrs pushed a commit to chivesrs/DefinitelyTyped that referenced this pull request Nov 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Owner Approved A listed owner of this package signed off on the pull request. Perf: Same typescript-bot determined that this PR will not significantly impact compilation performance.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants