[V2] [Typescript] ValueType<Option> is not exported.
See original GitHub issueThe problem
I’m using React-Select v2 in typescript and the onChange
event handler has a first parameter with a the type ValueType<T>
which is declared like this in the typings:
export type ValueType<OptionType> = OptionType | OptionsType<OptionType> | null | undefined;
The problem is that, because of that I can’t declare my event handler as:
private handleChange = (selected: MyOption) => {
/** Code **/
}
Instead I have to declare it like this:
private handleChange = (selected?: MyOption | MyOption[] | null) => {
/** Code **/
}
I can also declare ValueType in my project, but that’s a bit too much.
Possible solution:
Maybe there are better ways but one of the solutions, which would still be a bit awkward, would be to export the ValueType
type so we can at least use that and not declare the value type in that manner.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:27
- Comments:23 (4 by maintainers)
Top Results From Across the Web
Typescript errors when importing a "not exported" type, even ...
Typescript 3.8 introduces new syntax for type-only imports and exports: import type T from './module'; import type { A, B } from '....
Read more >Documentation - Modules - TypeScript
Often modules extend other modules, and partially expose some of their features. A re-export does not import it locally, or introduce a local...
Read more >Announcing TypeScript 4.7 - Microsoft Developer Blogs
Today we're excited to announce the availability of TypeScript 4.7! If you're not yet familiar with TypeScript, it's a language that builds ...
Read more >Working with the AWS CDK in TypeScript
This is the AWS CDK v2 Developer Guide. The older CDK v1 entered maintenance on June 1, 2022 and will now receive only...
Read more >Testing Non-Exported Functions in JavaScript - Samantha Ming
Testing Exported Function; Non-export function; Introducing Rewire ... Step 1: Install package; Step 2: Add to babel config; Step 3: Using it. Non-exported...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I had the same issue when using a select that allows single-select only. I was getting the same error as @bradchristensen.
I solved this by defining a type and doing a type assertion before using the selected option.
Hit this issue too and found this thread in my search for answers. The suggestions above didn’t resolve the issue for me, but I think helped point me in the right direction!
I was super confused about this for a while and then I realised it’s pretty easy to solve with a type check before you attempt to use the
selectedOption
. If you explicitly check whetherselectedOption
is an array, TypeScript then understands that it is not possible for the value to be an array from that point forward and allows you to use it like you expect to.For reference, below is the error I was getting, which goes away when the
Array.isArray
check is uncommented: