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

Typescript, FormDataConsumer, getSource is possibly undefinded #8958

Closed
PavelTankovich opened this issue May 31, 2023 · 3 comments · Fixed by #8979
Closed

Typescript, FormDataConsumer, getSource is possibly undefinded #8958

PavelTankovich opened this issue May 31, 2023 · 3 comments · Fixed by #8979

Comments

@PavelTankovich
Copy link

What you were expecting:
I expect the getSource function from FormDataConsumer not to be undefined

What happened instead:
When I try to use getSource from FormDataConsumer I get a problem related to ts.
TS2722: Cannot invoke an object which is possibly 'undefined'.
Because of this I have to add an extra condition in the code.

Steps to reproduce:
Use getSource from FormDataConsumer in an application with ts

Related code:

<FormDataConsumer>
  {({ scopedFormData, getSource, ...rest }) => scopedFormData.id !== 'BadId' && <MyInputs source={getSource('inputsList')} />}
</FormDataConsumer>;

Other information:

Environment

  • React-admin version: ^4.9.0
  • React version: ^18.2.0
  • Browser: all
  • Typescript: ^5.0.2
@slax57
Copy link
Contributor

slax57 commented Jun 1, 2023

getSource is only provided if <FormDataConsumer> is used inside a <SimpleFormIterator>, that's why it's typed as getSource?: (source: string) => string;

Besides, we don't have any TS warning in the simple demo (https://stackblitz.com/github/marmelab/react-admin/tree/master/examples/simple?file=src%2Fposts%2FPostEdit.tsx%3AL168)

So I'm not sure why you encountered that error.
Can you provide a reproduction?

@PavelTankovich
Copy link
Author

@slax57 Sure, here's an example, please take a look, https://codesandbox.io/p/sandbox/gifted-hooks-162e3r?file=%2Fsrc%2FExample%2Findex.tsx%3A18%2C68-18%2C77
You can see ts error
image

@slax57
Copy link
Contributor

slax57 commented Jun 5, 2023

@PavelTankovich Thanks for the repro.

I've played around a bit with it, and it turns out, this is dependent on the TS config in place.

I haven't figured out which config property yet, but as soon as you add a tsconfig.json file, even with the bare minimum content like

{
  "compilerOptions": {
    "rootDir": "src",
    "jsx": "react",
    "allowSyntheticDefaultImports": true
  },
  "exclude": ["**/*.spec.ts", "**/*.spec.tsx", "**/*.spec.js"],
  "include": ["src"]
}

then the error disappears and the IDE considers the type to be (source: string) => string. I'm not sure why.

Anyway, I don't see how we could improve the type for now, so the best we could do is mention this gotcha in the documentation.

Hence I'll label this as a documentation issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants