question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[V2] [Typescript] ValueType<Option> is not exported.

See original GitHub issue

The 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:closed
  • Created 5 years ago
  • Reactions:27
  • Comments:23 (4 by maintainers)

github_iconTop GitHub Comments

113reactions
plotkacommented, Apr 9, 2019

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.

import { ValueType } from "react-select/lib/types";

type OptionType = { label: string; value: number };

<Select
  onChange={(selectedOption: ValueType<OptionType>) => {
    const value = (selectedOption as OptionType).value;
    ...
  }}
  ...
/>
22reactions
bradchristensencommented, Feb 13, 2019

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 whether selectedOption 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.

<Select
  onChange={selectedOption => {
    if (Array.isArray(selectedOption)) {
      throw new Error("Unexpected type passed to ReactSelect onChange handler");
    }

    doSomethingWithSelectedValue(selectedOption.value);
  }}
  ...
/>

For reference, below is the error I was getting, which goes away when the Array.isArray check is uncommented:

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found