downshift-js / downshift

🏎 A set of primitives to build simple, flexible, WAI-ARIA compliant React autocomplete, combobox or select dropdown components.
http://downshift-js.com/
MIT License
12.05k stars 933 forks source link

TypeScript: have items and selectedItems as Readonly #1030

Open silviuaavram opened 4 years ago

silviuaavram commented 4 years ago

Relevant code or config

What you did:

What happened:

Reproduction repository:

Problem description: Since we use items/selectedItems just for some logic and we do not tamper with them, we can mark them as read only in the TS interface.

Suggested solution: In typings.ts for useSelect, useCombobox and useMultipleSelection, use Readonly to wrap the items type.

silviuaavram commented 4 years ago

Maybe it's a bit extreme for now so I will close it. If issue will arise in the future we can develop it then.

valtism commented 1 year ago

Hey, just letting you know that I am having issues with this. I would like to be able to pass an array of items marked with as const into the downshift items property, but it won't let me even though it gives me better types as outputs when I have it marked as such.

I think marking them as readonly would help a lot and I don't think it would break anything because it's technically a looser type definition.

valtism commented 1 year ago

@silviuaavram I tried to make a PR to address this just now but I don't know how to test the types. Running test:ts seems to not return any errors even if I break the types on purpose. Is there a proper way to test type changes?

silviuaavram commented 1 year ago

There is no automated test for the types. Just change the types in typings/index.d.ts and then check the usage.