JedWatson / JedWatson/react-select

Not possible to set optional props to undefined when `exactOptionalPropertyTypes: true` in tsconfig

Open
#5,785 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

issue/bug-unconfirmed
Dominant language
TypeScript
Stars
28k
Forks
4.1k
PR merge metrics
No merged PRs in 30d

Description

If I have exactOptionalPropertyTypes: true (docs) set in tsconfig, it treats id?: string and id?: string | undefined differently. This causes a problem that I am unable to set many props to undefined (e.g. id={undefined}) in the <Select> component.

Here is a reproduction of the problem: https://stackblitz.com/edit/vitejs-vite-yn3jwc?file=src%2FApp.tsx. After the app has loaded, you can see that there is the following TypeScript error:

Type '{ id: undefined; value: { value: string; label: string; } | null | undefined; options: { value: string; label: string; }[]; onChange: Dispatch<SetStateAction<{ value: string; label: string; } | null | undefined>>; }' is not assignable to type 'Omit<PublicBaseSelectProps<{ value: string; label: string; } | undefined, false, GroupBase<{ value: string; label: string; } | undefined>>, StateManagedPropKeys>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
  Types of property 'id' are incompatible.
    Type 'undefined' is not assignable to type 'string'.

I suggest that we change all optional props to also accept an explicit undefined. So in practice, change occurrences of key?: MyType to key?: MyType | undefined. I can try making a pull request, if some maintainer confirms that this should be fixed 👍

This is purely a TypeScript type issue, it doesn't affect the runtime at all.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked StackBlitz reproduction and the Select component's public prop type definitions, focusing on optional properties such as id under exactOptionalPropertyTypes: true. Done means explicit undefined is accepted by the affected props without changing runtime behavior, with the reproduction's TypeScript error resolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.